4

I am using a while loop

      $i = 0;
      $arr = array();
      while($get_key1 = mysql_fetch_assoc($get_key))
      {
        $busid = $get_key1['busid'];
        $get_key2 = mysql_query("select * from `route` where `busid`='$busid'") or die(mysql_error());

        while($get_key3 = mysql_fetch_assoc($get_key2))
        {
            $arr[$i] = $get_key3['routid'];
            echo "<pre>";
            print_r($arr);
        }
        echo "<hr/>";
        $i++;
      }

This inner loop gives three values, 1,3,4 and when itereates again then it gives values 1,4 I want to create a multi dimensional array as

array(array(1,3,4),array(1,4))

but the above code gives the result as :

Array
(
    [0] => 1
)

Array
(
    [0] => 1
    [1] => 3
)

Array
(
    [0] => 1
    [1] => 3
    [2] => 4
)

Array
(
    [0] => 1
    [1] => 3
    [2] => 4
    [3] => 1
)

Array
(
    [0] => 1
    [1] => 3
    [2] => 4
    [3] => 1
    [4] => 4
)

How can I store the values through while loop in multi dimensional array

1
  • 2
    change $arr[$i] to $arr[$i][] Commented Dec 16, 2015 at 5:05

1 Answer 1

3

You need to use $i as outer array and let create index of inner array itself.

while($get_key3 = mysql_fetch_assoc($get_key2))
{
    $arr[$i][] = $get_key3['routid']; // simple change
    echo "<pre>";
    print_r($arr);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Disha it saved me

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.