开发者

Building a multidimensional associative array using PHP and MYSQL

开发者 https://www.devze.com 2023-01-15 16:34 出处:网络
I\'m trying to build a multidimensional associative array while fetching the results from a MySQL query.

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",
   开发者_C百科 "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


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.


The way I build multidimensional arrays is through classes and their objects.

Example:

class Categories
{
    public $cat_name;
    public $id_cat;

    function __construct($cat_name, $id_cat)
    {
        $this->id = $id_cat;
        $this->name = $cat_name;
        $this->sub_cat = array();
    }       
}

class SubCategories
{
    public $name_sub_cat;
    public $id_sub_cat;

    function __construct($name_sub_cat, $id_sub_cat)
    {
        $this->id = $id_sub_cat;
        $this->name = $name_sub_cat;
        $this->ss_cat = array();
    }       
}

class SSCategories
{
    public $ss_cat_name;
    public $id_ss_cat;

    function __construct($ss_cat_name, $id_ss_cat)
    {
        $this->id = $id_ss_cat;
        $this->name = $ss_cat_name;
    }       
}

Then to write to those classes (after fetching MySQL DB Data [assuming OOP mysql]):

if($result)
{
    $array = array();
    while($row = $result->fetch_array())
    {
        $array[] = new Categories($row["cat_name"], $row["id_cat"]);
    }

    foreach($array as $main)
    {
        ... (fetching sub category info)
        if($sub_result)
        {
            $sub_array = array();
            while($sub_row = $sub_result->fetch_array())
            {
                $sub_array[] = new SubCategories($sub_row["name_sub_cat"], $sub_row["id_sub_cat"]);
            }
            $main->sub_cat = $sub_array;

You would repeat for each within original if loop for as many sub arrays as you have. This took a while :).

NOTE: Close if loops (heirarchally) if there are no more sub arrays.

echo '<pre>';
print_r($array);
echo '</pre>';

The above code will help show the levels of your array!

0

精彩评论

暂无评论...
验证码 换一张
取 消