You are here: Re: Help with array « PHP Programming Language « IT news, forums, messages
Re: Help with array

Posted by aarondeloach on 03/12/07 04:19

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация