Building a multidimensional associative array using PHP and MYSQL

staffybtuk

New Member
I'm trying to build a multidimensional associative array while fetching the results from a MySQL query.But i can't figure out how to achieve this. I want to build an array like this one:\[code\] array ("cat_name" => "category1", "id_cat" => "1", "sub_cat" => array ("name_sub_cat" => "subCategory","id_sub_cat" => "4", "ss_cat" =>array ("ss_cat_name" =>"ss_cat1", "id_ss_cat" => "4" ) ) );\[/code\]Here's where i'm building the array:
Edit : I've ended up with something like that, not very sexy, but if think i'm almost there\[code\]while($row = mysql_fetch_assoc($resultQueryCat)){ $menuArray[$row["id_cat"]]["cat_name"] = $row["cat_name"]; $menuArray[$row["id_cat"]][$row["id_sub_cat"]]["id_sub_cat"] = $row["id_sub_cat"]; $menuArray[$row["id_cat"]][$row["id_sub_cat"]]["name_sub_cat"] = $row["name_sub_cat"]; $menuArray[$row["id_cat"]][$row["id_sub_cat"]][$row["ss_cat_name"]]["ss_cat_name"] = $row["ss_cat_name"]; $menuArray[$row["id_cat"]][$row["id_sub_cat"]][$row["ss_cat_name"]]["id_ss_cat"] = $row["id_ss_cat"]; }\[/code\]Edit2: The code to display the array\[code\] $menu.='<ul>'; foreach ($menuArray as $key) { $compteur_cat++; $menu.= '<div id="collapsiblePanelCol'.$compteur_cat.'" class="collapsiblePanelCol floatleft"> <li class="categorie"> '.$key["cat_name"].' <ul>'; foreach ($key as $key1) {if (is_array($key1){/* One of the thing i forgot which totally screwed up my results*/ $menu.= '<ul> <li class="ss_categorie">'.$key1["name_sub_cat"].'<ul>'; foreach ($key1 as $key2) { if (is_array($key2)){ $menu.= '<li class="element">'.$key2["ss_cat_name"].'</li>'; } } $menu.= '</ul></li></ul>'; } }$menu.='</ul> </li> </div>'; } $menu.= '</ul>';\[/code\]Thanks.Final Edit: My code is working :) i edited the previous code to make it correct
 
Back
Top