Search code examples
phparraysarray-push

php - push array into an array -(pushing both key and the array)


I am trying to add an array to an existing array. I am able to add the array using the array_push . The only problem is that when trying to add array that contains an array keys, it adds an extra array within the existing array.

It might be best if I show to you

foreach ($fields as $f)
{  
    if ($f == 'Thumbnail')
    {
        $thumnail = array('Thumbnail' => Assets::getProductThumbnail($row['id'] );
        array_push($newrow, $thumnail);
    }
    else
    { 
        $newrow[$f] = $row[$f];
    }
}

The fields array above is part of an array that has been dynamically fed from an SQl query it is then fed into a new array called $newrow. However, to this $newrow array, I need to add the thumbnail array fields .

Below is the output ( using var_dump) from the above code. The only problem with the code is that I don't want to create a seperate array within the arrays. I just need it to be added to the array.

 array(4) { ["Product ID"]=> string(7) "1007520"
           ["SKU"]=> string(5) "G1505"
           ["Name"]=> string(22) "150mm Oval Scale Ruler"            
           array(1) { ["Thumbnail"]=> string(77) "thumbnails/products/5036228.jpg" } }

I would really appreciate any advice.


Solution

  • All you really want is:

    $newrow['Thumbnail'] = Assets::getProductThumbnail($row['id']);