-1

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)
    }
}
2
  • 3
    Can you include a sample of the multi-dimensional array data? Commented Nov 30, 2011 at 18:13
  • 1
    Check my most recent edit. I am pretty sure that should work. Commented Nov 30, 2011 at 18:24

3 Answers 3

1

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

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

2 Comments

$this->productToShow = key($this->shoppingBag);
that worked. thanks seems to be as simple indeed, I think i'm doing way to much time working with those arrays today. thanks for the help.
0

Try:

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

1 Comment

Is the count one of the variables in the array? Or is the count the number of elements in the array?
0

$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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.