up vote 1 down vote favorite

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

link|flag
Fix up your code so its legible. – Chris Sep 8 at 12:16

1 Answer

up vote 0 down vote accepted

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.

link|flag
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 Sep 7 at 11:06
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 Sep 7 at 11:12
Ok, I'm not on my computer right now, i'll provide some code to complete my question ASAP. – rcampistron Sep 7 at 11:25
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 Sep 7 at 14:33
print_r is your friend. – BarsMonster Sep 7 at 14:43
show 4 more comments

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.