Monday, 27 August 2018

PHP multidimensional array key value

I am working with multidimensional arrays; I use the arsort function to get the array that has been added the latest. This all works fine
arsort($this->shoppingBag);
$this->productToShow = key($this->shoppingBag);

When i want to use this array i do:
$prodName = key($this->shoppingBag[$this->productToShow]);

this gives me the correct product with the correct name that I need. When i do
$count = $this->shoppingBag[$this->productToShow[$prodName]];

It gives me an "Undefined index" error.
When I echo the array with the key as a string i get the correct value from that array..
Why is this and how can I get the value with that key?
edit:
array(4)
{
    [38] => array(1)
    {
        ["SAMSUNG LE32D450"] => int(3)
    }
    [32] => array(1)
    {
        ["Inspiron 15R"] => int(1)
    }
    [29] => array(1)
    {
        ["XPS 15"] => int(25)
    }
    [37] => array(1)
    {
        ["Logitech M185 Black"] => int(10)
    }
}

Is this as simple as $this->productToShow is just a key variable and not an array. So the call to an index of that variable is undefined. Then wouldn't the answer you are looking for be $count = $this->shoppingBag[$this->productToShow][$prodName];.

0 comments:

Post a Comment