473,382 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Help with array

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
8 2332
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
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
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Treetop | last post by:
I am creating a calendar with JavaScript. I want to be able to put date and time in an array and have a calendar display properly with 7 columns. I am stuck at this point. Any help would be...
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
5
by: ritchie | last post by:
Hi, I am writing to ask if anyone can see why my array is not being sorted correctly? It's an array of 4 elements(ints 1,2,3,4) but after calling the selection sort it comes back sorted as...
27
by: ruel loehr | last post by:
Hey guys, I am looking for some insight: Given two sorted arrays of integers A and B, where array B has enough extra room in it to hold the contents of both A and B. Merge array A and B...
3
by: ash | last post by:
Hey I am new, but I don't have time to intruduce myself yet. I am intro to C++ and this is a programme I have to write. all the direction are here, It will be very nice of someone to figure this...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
3
by: inkexit | last post by:
I need help figuring out what is wrong with my code. I posted here a few weeks ago with some code about creating self similar melodies in music. The coding style I'm being taught is apparently a...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.