Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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)
    }
}
share|improve this question
3  
Can you include a sample of the multi-dimensional array data? –  Pastor Bones Nov 30 '11 at 18:13
    
yes, array is now included. –  turncoat Nov 30 '11 at 18:24
    
Check my most recent edit. I am pretty sure that should work. –  Laurence Burke Nov 30 '11 at 18:24
    
Okay, that worked great! thanks all –  turncoat Nov 30 '11 at 18:27
add comment

3 Answers

up vote 1 down vote accepted

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

share|improve this answer
    
$this->productToShow = key($this->shoppingBag); –  turncoat Nov 30 '11 at 18:24
    
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. –  turncoat Nov 30 '11 at 18:26
1  
@turncoat then dont forget to accept the answer. –  Laurence Burke Nov 30 '11 at 18:26
    
i had to wait a few minutes :) –  turncoat Nov 30 '11 at 18:27
    
@turncoat thanks and GL. –  Laurence Burke Nov 30 '11 at 18:28
add comment

Try:

$count = $this->shoppingBag[$this->productToShow];
share|improve this answer
    
this gives me the array –  turncoat Nov 30 '11 at 18:22
    
Is the count one of the variables in the array? Or is the count the number of elements in the array? –  Brian Fisher Nov 30 '11 at 18:24
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.