views:

50

answers:

1

Hi all, 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:

 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"
                           )
       )
  );

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

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"];
                                     }

Edit2: The code to display the array

    $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>';

Thanks.

Final Edit: My code is working :) i edited the previous code to make it correct

A: 

It's not an easy task, as data from DB might contain loops :-)

I'd better save it in id->data hashmap so that you can build the structure easier.

Not sure why you need multidimensions.

BarsMonster
Thanks for the insight, I need to build a nested menu, and i thought a multidimensional array would be the best solution.I'm quite new to php, and i don't see what are hashmaps. Could you provide some info, or maybe i'll just ask my friend google ;)
rcampistron
Well, any array is hashmap actually. So to put whole row in an array I would do this: $data[$row[id]] = $row; and in the beginning initialization is the following: $data = array();
BarsMonster
Ok, I'm not on my computer right now, i'll provide some code to complete my question ASAP.
rcampistron
I made some edits, it seems to be what i'm looking for, but now i'm having difficulties to echo the whole thing neatly.
rcampistron
print_r is your friend.
BarsMonster
I edited my code to show how i display the menu
rcampistron
Thanks for your help BarsMonster, my code is working. I just forgot to check whether the value was an array or not before printing it, thus ending with some weird results.
rcampistron
Well, at some point you are supposed to accept an answer :-)
BarsMonster
What can i do? Even if your advice was useful, it can't be considered as an answer. I'd have upticked (upvoted?) it if i could (not enough rep) :)
rcampistron