0

I have been trying to fureout how to create a dynamic multi dimensional array.

The reason for this is that I want to create a dropdown menu that will be created dynamically from the mysql

Sample database

|id|menu_name|menu_parent_id|

|1 |top menu1|      0       |

|2 |top menu2|      0       |

|3 |top menu3|      0       |

|4 |top menu4|      0       |

|5 |sub menu |      2       |

|6 |sub menu |      2       |

|7 |sub menu |      5       |

|8 |sub menu |      6       |

|9 |top menu |      0       |

I thought of starting from getting the menu with no parent then put it on an array

$parentIDs[0]=0;
$tempParentIDs = array();
$childIDs = array();
$menus = array();
$rows = 0;

$rows=0;
foreach($parentIDs AS $value){
$sql = mysql_query("SELECT * FROM service WHERE service_parent_id=$value");
while($temp = mysql_fetch_array($sql)){

    //$tempParentIDs[] = $temp['service_id'];

    //check if parent have child
    $sql2 = mysql_query("SELECT * FROM service WHERE  service_parent_id=$temp[service_id]") or die(mysql_error());
    $rows = mysql_num_rows($sql2);
    if($rows >= 1){
        //This means there is a child
        while($temp2 = mysql_fetch_array($sql2)){
            $childIDs[] = $temp2['service_id'];
        }

        $tempParentIDs[$temp['service_id']] = $childIDs;
        unset($childIDs);
    } else {
        //This means there is no child
    }
}
}

echo "<pre>";
print_r($tempParentIDs);
echo "</pre>";

but after then I'm stuck.

2
  • what you need additional in your multidimensional array? Commented Nov 16, 2013 at 5:54
  • @VijayVerma, I just want to create a menu from the mysql Commented Nov 16, 2013 at 8:16

2 Answers 2

1

I think you are looking for this:

$parentIDs[0]=0;
$tempParentIDs = array();
$childIDs = array();
$menus = array();
$rows = 0;
$multiDimensionalArray = NULL; //here I made change - vijay

$rows=0;
foreach($parentIDs AS $value){
$sql = mysql_query("SELECT * FROM service WHERE service_parent_id=$value");
while($temp = mysql_fetch_array($sql)){

    $tempParentIDs = $temp['service_id']; //here I made change - vijay

    //check if parent have child
    $sql2 = mysql_query("SELECT * FROM service WHERE  service_parent_id=$temp[service_id]") or die(mysql_error());
    $rows = mysql_num_rows($sql2);
    if($rows >= 1){
        //This means there is a child
        while($temp2 = mysql_fetch_array($sql2)){
            $multiDimensionalArray[$tempParentIDs][] = $temp2['service_id']; //here I made change - vijay
        }

       // $tempParentIDs[$temp['service_id']] = $childIDs; //here I made change - vijay
       // unset($childIDs); //here I made change - vijay
    } else {
        //This means there is no child
    }
}
}

echo "<pre>";
print_r($multiDimensionalArray); //here I made change - vijay
echo "</pre>";
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Vijay, however this only reaches until level two of the array.. If you take a look at it you can see that one of the record is actually under a sub menu |8 |sub menu | 6 |
It is not possible easily in loop, you need recursion for that.
I see. So instead of searching for loop? THank you for pointin me on the right direction!
0

You should store array in php like this way : This will maybe work for you

$next = 0;

$level = 0;

$sql = mysql_query("SELECT * FROM service WHERE service_parent_id=$next");

$temps = array();

while($temp = mysql_fetch_array($sql))
{
$temps[] = $temp;
}

foreach($temps as $temp)
{
echo $temp['service_id'];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.