1

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.

2
  • 1
    This defeats the purpose of an array in the first place. Each iteration of your foreach loop is going to overwrite the last set of variables since the name is the same. What are you ultimately trying to achieve? What do you intend to do with these values as variables? Commented Jul 22, 2017 at 4:41
  • @NathanDawson Yes I know it will overwrite the last variables but it's okay because in each looping I insert the value of variable in database. What I want to know is how to get the value of the second array inside that array ? Commented Jul 22, 2017 at 4:43

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

I don't realize if that is very simple. Thanks.
2

you can use recursive for this.

class ProductData
{
    public function walkRecursive($a) {
        asort($a); //Sort top down
        foreach($a as $k => $v){
            if(is_array($v)){
                $this->walkRecursive($v);
            }else{
                $this->{$k} = $v;                       
            }
        }

        return $this;
    }

}

$array = array(
    "SKU" => "Test1",
    "Warehouse" => array("WID" => 1, "Quantity" => 2),
    "Name" => "Product1"
);


$d = new ProductData();
$product = $d->walkRecursive($array);

You can now do: $product->SKU. Or $product->Quantity or $Product->Name

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.