Re: Array... Parent IDs...

From: Date: Tue, 08 Apr 2003 07:43:00 +0000
Subject: Re: Array... Parent IDs...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-142812@lists.php.net to get a copy of this message
On Tue, 2003-04-08 at 01:31, Michael Smith wrote: > $_tmpArray: > > Array > ( > [2] => Array > ( > [parent_id] => 1 > ) > [3] => Array > ( > [parent_id] => 3 > ) > ) > > Of course it is much, much larger, and has some more values, however, > that's not important. I'm trying to figure out a way to group these into > arrays based upon their parent_id. I want all the ones with a parent_id > of 1 in an array called $pid1 (or it could be $pid[1], that would be > fine) and all with a parent_id of two in another array, etc. I'm trying > to figure out exactly how to do this... I need to be able to create a > tree based on the parent_ids (in HTML)... is there a way to do this? I'm > using similar code to get it from the database as phpLayersMenu, and > they print it as javascript, but the order doesn't matter... > This is untested, but it should do what you want : while(list($key,$val) = each ($_tmpArray)) { if (is_array($val)) { while (list($k,$v) = each ($val)) { if ($k == "product_id" && !is_array(${"pid_$v"}) { $n = $v; ${"pid_$v"} = array(); continue; } else { ${"pid_$n"}[] = $v; } } } } -- Burhan Khalid phplist[at]meidomus[dot]com Please add a reply-to:php-general@lists.php.net header in your emails

« previous php.general (#142812) next »