By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,760 Members | 1,557 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,760 IT Pros & Developers. It's quick & easy.

Help with array

P: n/a
I have an array of id=>category:

[018821] =Automotive Parts
[222033] =Automotive Parts\Domestic
[205884] =Automotive Parts\Foreign
[987219] =Building Supplies
[624668] =Building Supplies\Lumber
[063964] =Building Supplies\Lumber\Hardwood
[592999] =Building Supplies\Lumber\Hardwood\Steel
[987808] =Building Supplies\Lumber\Structural
[169626] =Building Supplies\Lumber\Treated
[966554] =Building Supplies\Plumbing
[329645] =Construction Adhesives
[621126] =Roofing Supplies
[400762] =Roofing Supplies\Asphalt Roofing
[475138] =Roofing Supplies\Metal Roofing

What I'm trying to do is loop through the array and create a category
tree:

Automotive Parts
Domestic
Foreign
Building Supplies
Lumber
Hardwood
Steel
Structural
Treated
Plumbing
Construction Adhesives
Roofing Supplies
Asphalt Roofing
Metal Roofing

Any guidance?

Regards,
Aaron

Mar 11 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
aa***@deloachcorp.com wrote:
I have an array of id=>category:

[018821] =Automotive Parts
[...]
What I'm trying to do is loop through the array and create a category
tree:

Automotive Parts
Domestic
Foreign
[...]
Any guidance?
Get the position of the last slash in the array value, run a in_array(), get
the parent ID...

That is pretty inefficient, so once you got the information about the parnt
of each node, store it in the DB (I suppose you're using a DB backend).
When fetching from the DB, rebuild the tree (remember you can use nested
arrays as trees), and everything will be much easier from there.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Beware of low-flying butterflies.

Mar 11 '07 #2

P: n/a
On Mar 10, 9:54 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
a...@deloachcorp.com wrote:
I have an array of id=>category:
[018821] =Automotive Parts
[...]
What I'm trying to do is loop through the array and create a category
tree:
Automotive Parts
Domestic
Foreign
[...]
Any guidance?

Get the position of the last slash in the array value, run a in_array(), get
the parent ID...

That is pretty inefficient, so once you got the information about the parnt
of each node, store it in the DB (I suppose you're using a DB backend).
When fetching from the DB, rebuild the tree (remember you can use nested
arrays as trees), and everything will be much easier from there.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Beware of low-flying butterflies.
No DB backend. This tree needs to be created on-the-fly from the array
provided. Ishould note that I'm usung PHP 4+ so array_walk_recursive
won't be available.

Tanks for your reply.

Mar 11 '07 #3

P: n/a
aarondeloach wrote:
No DB backend. This tree needs to be created on-the-fly from the array
provided.
No way. Doing so, on-the-fly, every time, is:
a) a waste of CPU time (too inefficient)
b) consecuently, a decrease of the server performance
c) complex code
d) consecuently, a source of bugs
e) consecuently, a source of headaches
So, whatever backend you're using, modify it to store the tree. And run a
script, *once* to convert the plain array into the tree.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Now listening to: Orbital - Diversions (1994) - [5] Lush 3 - 4 Warrior Drift
Psychick Warriors Ov Gaia (10:49) (95%)
Mar 11 '07 #4

P: n/a
Well, if you still want to know how to do it, you could use the string
tokenizer instead of finding the last position of the slash and then
extracting it. The string tokenizer function is strtok(). Look in the
PHP documentation for how to use it.

Mar 11 '07 #5

P: n/a
On Mar 11, 8:18 am, "Klarth" <kah....@gmail.comwrote:
Well, if you still want to know how to do it, you could use the string
tokenizer instead of finding the last position of the slash and then
extracting it. The string tokenizer function is strtok(). Look in the
PHP documentation for how to use it.
I still can't get the gist of "finding the last position of the slash"
and how it would work. Could you provide an example of what you're
referring to?

Mar 12 '07 #6

P: n/a
"aarondeloach" posted...
<snip>
[018821] =Automotive Parts
[222033] =Automotive Parts\Domestic
[205884] =Automotive Parts\Foreign
[987219] =Building Supplies
[624668] =Building Supplies\Lumber
[063964] =Building Supplies\Lumber\Hardwood
[592999] =Building Supplies\Lumber\Hardwood\Steel
[987808] =Building Supplies\Lumber\Structural
[169626] =Building Supplies\Lumber\Treated
[966554] =Building Supplies\Plumbing
[329645] =Construction Adhesives
[621126] =Roofing Supplies
[400762] =Roofing Supplies\Asphalt Roofing
[475138] =Roofing Supplies\Metal Roofing
</snip>

// constants are optional... recommended, and an editor such
// as SciTE recommended when messing with such things.
// SciTE is easily configurable to handle ALL your constants
// and functions and can easily provide Intellisense to help
// with your software development.
define("AUTOMOTIVE", 0);
define("BUILDING", 1);
define("ADHESIVES", 2);
define("ROOFING", 3);
define("LUMBER", 4);
define("HARDWOOD", 5);
define("HARDWOOD_STEEL", 6);
define("STRUCTURAL", 7);
define("TREATED", 8);
define("PLUMBING", 9);
define("ROOFING", 10);
define("ROOFING_ASPHALT", 11);
define("ROOFING_METAL", 12);

// constants for fields
define("INV_INDEX", 0);
define("INV_CATEGORY", 1);
define("INV_PARENT", 2);
define("INV_ID", 3);

// index, category_name, parent_category, id_number
$aCategories = array(
array(0, "Inventory", 0, 0),
array(1, "Automotive Parts", 0, 018821),
array(2, "Foreign", 1, 205884),
array(3, "Domestic", 1, 222033),
array(4, "Building Supplies", 0, 987219),
array(5, "Construction Adhesives", 0, 329645),
array(6, "Roofing Supplies", 0, 621126),
array(7, "Lumber", 4, 400762),
array(8, "Hardwood", 7, 063964),
array(9, "Steel", 8, 592999),
array(10, "Structural", 7, 987808),
array(11, "Treated", 7, 169626),
array(12, "Plumbing", 4, 966554),
array(13, "Asphalt Roofing", 6, 400762),
array(14, "Metal Roofing", 6, 475138)
);

Those represent some possible suggestions.

Other than that, there's always the split() function if you need
regular expression abilities, or the explode() function for quick
and easy tearing up strings with slashes...

http://us2.php.net/manual/en/function.explode.php
http://us2.php.net/manual/en/function.split.php

Hope this helps.

--
Jim Carlock
Post replies to the group.
Mar 12 '07 #7

P: n/a
I originally posted:
>I have an array of id=>category:

[018821] =Automotive Parts
[222033] =Automotive Parts\Domestic
[205884] =Automotive Parts\Foreign
[987219] =Building Supplies
[624668] =Building Supplies\Lumber
[063964] =Building Supplies\Lumber\Hardwood
[592999] =Building Supplies\Lumber\Hardwood\Steel
[987808] =Building Supplies\Lumber\Structural
[169626] =Building Supplies\Lumber\Treated
[966554] =Building Supplies\Plumbing
[329645] =Construction Adhesives
[621126] =Roofing Supplies
[400762] =Roofing Supplies\Asphalt Roofing
[475138] =Roofing Supplies\Metal Roofing
What I'm trying to do is loop through the array and create a category
tree:

Automotive Parts
Domestic
Foreign
Building Supplies
Lumber
Hardwood
Steel
Structural
Treated
Plumbing
Construction Adhesives
Roofing Supplies
Asphalt Roofing
Metal Roofing
Thanks for all the replies. I've created a solution. Maybe it can be
refined, but it's working:

function categories_to_html(&$categories){
global $path;
$temp = array();
$html='<div class="heading">Browse Items</div>';
$first = true;
foreach ($categories as $id =$str) {
$fields = explode('\\',$str);
$current = &$temp;
$space ="&nbsp;&nbsp;&nbsp;";
$i=0;
foreach ($fields as $field) {
if (!array_key_exists($field, $current)) {
$current[$field] = array();
$html.=(!$first && $i==0 ? '&nbsp;<br>' : '').
str_repeat($space,$i).($i==0 ? '<b>' : '&raquo; ').
'<a href="'.$path.'mod/store.php?filter='.$id.'&sort='.
$_REQUEST['sort'].'">'.$field.'</a>'.
($i==0 ? '</b>' : '')."<br>";
}
$first=false;
$i++;
$current = &$current[$field];
}
}
return $html.'<br>&nbsp;';
}

Regards,
Aaron
Mar 12 '07 #8

P: n/a
Jim Carlock posted...
<snip>
// constants are optional... recommended, and an editor such
// as SciTE recommended when messing with such things.
// SciTE is easily configurable to handle ALL your constants
// and functions and can easily provide Intellisense to help
// with your software development.
define("AUTOMOTIVE", 0);
define("BUILDING", 1);
define("ADHESIVES", 2);
define("ROOFING", 3);
define("LUMBER", 4);
define("HARDWOOD", 5);
define("HARDWOOD_STEEL", 6);
define("STRUCTURAL", 7);
define("TREATED", 8);
define("PLUMBING", 9);
define("ROOFING", 10);
define("ROOFING_ASPHALT", 11);
define("ROOFING_METAL", 12);

</snip>

I referenced SciTE and forgot to provide a link to it. It's
a really great editor for many languages out there.

http://sourceforge.net/projects/scintilla/

Scintilla is the DLL (library behind) for SciTE and there's
a few popular commercial PHP editors that sell themselves
using the Scintilla DLL.

Glad to hear you got your stuff worked out.

--
Jim Carlock
Post replies to the group.
Mar 12 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.