Re: Array headaches

From: Date: Fri, 04 Aug 2000 21:12:50 +0000
Subject: Re: Array headaches
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-10194@lists.php.net to get a copy of this message
This might take a little tweaking but I think it will do what you want: Table Defination: id, parent_id (-> id), name ------------------------------------------------------------------ function getJobCategories($firstEle = "" ) { if( $firstEle != "" ) { $ary[] = $firstEle; } $query = " SELECT id,parent,name FROM lup_job_category ORDER BY name "; dbQuery($query, $res); while( $obj = dbFetchObj($res) ) { $tmpAry[$obj->parent][$obj->id] = $obj->name; } dbFreeResult($res); sortCategories($tmpAry, $ary, 0, ""); return($ary); } function sortCategories(&$inAry, &$outAry, $level, $indent) { if( is_array($inAry[$level]) ) { while( list($id, $name) = each($inAry[$level]) ) { $outAry[$id] = "$indent $name"; sortCategories($inAry, $outAry, $id, "$indent $name -- "); } } } ----------------------------------------------------------------- When you first call sortCategories, if you change the $indent argument to say " " you'll get indentation.... good luck! -philip In article <005101bffe42$ecbd2a60$0200ffc0@bealers>, Darren Beale <bealers@back-end.org> wrote: >Hi >can someone recommend a good resource for figuring out the >sorting/displaying of multi-dimensional arrays? >I've been @ this all day and I've got no closer to figuring it out > >the stage I'm @ now is: >=>select child,parent,name from table > >build up this array > >=> $cat_matrix[parent][child] = name > >i.e. > >$cat_matrix[0][1] = "Top Level"; >$cat_matrix[0][2] = "Top Level 2"; >$cat_matrix[0][3] = "Top Level 3"; >$cat_matrix[1][4] = "Top Level - Sub1"; >$cat_matrix[1][5] = "Top Level - Sub2"; >$cat_matrix[2][6] = "Top Level 2 - Sub1"; >$cat_matrix[2][7] = "Top Level 2 - Sub2"; >$cat_matrix[3][8] = "Top Level 3 - Sub1"; >$cat_matrix[7][9] = "Top Level 2 - Sub2 - SubSub1"; >$cat_matrix[9][10] = "Top Level 2 - Sub2 - SubSub1 - subsubsub1"; > >etc......... > >and I want to end up with a threaded layout on the screen > >Top Level >==Top Level - Sub1 >==Top Level - Sub2 > >Top Level 2 >==Top Level 2 - Sub1 >==Top Level 2 - Sub2 >====Top Level 2 - Sub2 - SubSub1 >======Top Level 2 - Sub2 - SubSub1 - subsubsub1 > >Top Level 3 >==Top Level 3 - Sub1 > >and it is doing my poor head in, now it's an interesting excersise, but I'm >at the stage where I need some input. > >Syntax is not an issue here, I've used arrays a million times before, it's >purely the logic that I'm falling down on > >has anyone come across a decent code example that might give me the flash of >inspiration that I need? > >tia > >Bealers

« previous php.general (#10194) next »