Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php pushing values to a 2-dimensional array

i've a 2-dimensional array and i want to push values to it with a while loop like;

   $arr[0][1] = 1. value
   $arr[0][2] = 2. value

i ve tried

   while($zRow = mysql_fetch_array($zQuery))
    {
     $props[]['name'] =$zRow['name'];
     $props[]['photo'] =$zRow['thumbnail'];
    }

this loop pushes name to $props[0][name] and thumbnail to $props[1][photo]

i also tried

   $j = 0;
   while($zRow = mysql_fetch_array($zQuery))
    {
     $props[$j]['name'] =$zRow['name'];
     $props[$j]['photo'] =$zRow['thumbnail'];
     $j+=1;     
    }

that works but with this i when i use foreach loop later, it makes trouble like "Illegal offset type"

and here is my foreach loop

    foreach($props as $no)
    {
      echo $props[$no]['name'];
    } 

now my questions; 1) are there any other way than while loop with $j variable like array_push for 2-dimensional arrays 2)how can i use foreach loop for 2-dimensional arrays

like image 547
Malixxl Avatar asked Feb 05 '12 22:02

Malixxl


People also ask

How can insert data in multidimensional array in PHP?

If we want to add values/elements in a multi-dimensional array. Here we will take an example for adding the values/elements in a multidimensional array. echo "After add the value:- " ; print_r( $array );

Can you push an array into an array PHP?

PHP array_push() function is used to insert new elements into the end of an array and get the updated number of array elements. You may add as many values as you need. Your added elements will always have numeric keys, even if the array itself has string keys. PHP array push function has been introduced in PHP 4.

What is array_push in PHP?

The array_push() function inserts one or more elements to the end of an array. Tip: You can add one value, or as many as you like. Note: Even if your array has string keys, your added elements will always have numeric keys (See example below).


2 Answers

You could change the first loop to the following:

while($zRow = mysql_fetch_array($zQuery))
{
    $row = array();
    $row['name'] = $zRow['name'];
    $row['photo'] = $zRow['thumbnail'];
    $props[] = $row;
}

Your method also works, but you need that extra variable.

In your second loop, what you actually need to be doing is:

foreach($props as $index => $array)
{
    echo $props[$index]['name'];
    // OR
    echo $array['name'];
}
like image 118
drew010 Avatar answered Oct 23 '22 11:10

drew010


Pushing anything onto an array with $myArray[] = 'foo' will increment the array's counter.

For multidimensional array, you need to populate the "inner" array, then push it to the "outer" (in your case $props) array.

while($zRow = mysql_fetch_array($zQuery)) {
    $data = array('name' => $zRow['name'], 'photo' => $zRow['thumbnail']);
    $props[] = $data;
}

To iterate over multidimensional arrays whose depth is known:

foreach ($props as $prop) {
    foreach ($prop as $key => $value) {
        echo "{$key} => {$value}" . PHP_EOL;
    }
}

If the depth of the nesting is not known, you may have to use a recursive function to gather the data.

like image 31
Andy Avatar answered Oct 23 '22 13:10

Andy