kendepelchin
kendepelchin

Reputation: 2974

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)
    }
}

Upvotes: -1

Views: 1546

Answers (3)

Chris C
Chris C

Reputation: 2023

$this->productToShow is not an array. So seeing as $prodName is the key of a particular item in $this->shoppingBag,

I am assuming that you want $count to return the last key Because if we omitted the incorrectly applied [$prodName] to the $this->productToShow key, $count will return the value of the last key in the shopping bag.

Upvotes: 0

Laurence Burke
Laurence Burke

Reputation: 2358

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];.

Upvotes: 1

Brian Fisher
Brian Fisher

Reputation: 23989

Try:

$count = $this->shoppingBag[$this->productToShow];

Upvotes: 0

Related Questions