Retrieves the value of array in array and stores it in variable

advertisements

I got some array inside array and I want to get all the value of that and store it on variable. This is the structure of my array :

array(5) {  ["InventoryID"]=> string(2) "43"
            ["Price"]=> string(5) "45.36"
            ["Warehouse"]=> array(2) {  ["WID"]=> string(1) "1"
                                        ["Quantity"]=> string(1) "12"
                                     }
            ["Name"]=> string(48) "Bottle"
            ["SKU"]=> string(8) "M123"
         }
array(5) {  ["InventoryID"]=> string(2) "51"
            ["Price"]=> string(5) "21.67"
            ["Warehouse"]=> array(2) {  ["WID"]=> string(1) "1"
                                        ["Quantity"]=> string(1) "40"
                                     }
            ["Name"]=> string(48) "Glass"
            ["SKU"]=> string(8) "M124"
         }

What I want is to get all the value of that array and store it in variable like this :

$inventoryid = 43;
$price = 45.36;
$wid = 1;
$quantity = 12;
$name = 'Bottle';
$sku = 'M123';

What I do till now is I looping that array in foreach but I can't get the second array value. This is what I do till now :

foreach($array as $row)
{
    $inventoryid = $row['InventoryID'];
    $price = $row['Price'];
    $name = $row['Name'];
    $sku = $row['SKU'];

    foreach($row['Warehouse'] as $row2)
    {
        $wid = $row2['WID'];
        $quantity = $row2['Quantity'];
    }
}

How can I do that ? Please anyone give me some code example with foreach to get the value.


I'm struggling to fully understand what benefit you're getting from having these values as variables rather than part of an array. You're still able to access the values quite easily.

Anyway, based on your comment it appears the issue is with the nested array. You don't need a foreach to access those values.

Example:

$wid = $row['Warehouse']['WID'];
$quantity = $row['Warehouse']['Quantity']

Documentation: http://php.net/manual/en/language.types.array.php