2011-11-30 225 views
0

我正在使用多维数组; 我使用arsort函数来获取已添加最新的数组。当我想使用这个数组我做的这一切工作正常PHP多维数组键值

arsort($this->shoppingBag); 
$this->productToShow = key($this->shoppingBag); 

$prodName = key($this->shoppingBag[$this->productToShow]); 

这给了我,我需要正确的名称正确的产品。当我做

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

它给了我一个“未定义的索引”的错误。

当我回声钥匙作为字符串我从阵列得到正确的值数组..

这是为什么,我怎么能得到与该键的值?

编辑:

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

您能否包含多维数组数据的示例? –

+0

是的,现在包含数组。 – kendepelchin

+1

检查我最近的编辑。我很确定这应该工作。 –

回答

1

这是一样简单$this->productToShow仅仅是一个关键变量,而不是一个数组。所以对该变量的索引的调用是未定义的。那么你不会找到答案是$count = $this->shoppingBag[$this->productToShow][$prodName];

+0

$ this-> productToShow = key($ this-> shoppingBag); – kendepelchin

+0

工作。谢谢 的确看起来很简单,我想我今天正在为这些阵列做很多事情。谢谢您的帮助。 – kendepelchin

+1

@turncoat然后不要忘记接受答案。 –

0

尝试:

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

这给了我数组 – kendepelchin

+0

是数组中的变量之一吗?或者计数数组中的元素数量? –

0

$this->productToShow不是数组。所以看到,因为$prodName$this->shoppingBag特定项目的关键,

我假设你想$ count来返回最后一个关键,因为如果我们忽略了正确应用[$prodName]$this->productToShow键,$数将返回值购物袋中的最后一把钥匙。