繁体   English   中英

PHP多维数组键值

[英]PHP multidimensional array key value

我正在处理多维数组; 我使用arsort函数来获取已添加了最新数组。 这一切都很好

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

是否像$this->productToShow一样简单,只是一个关键变量而不是数组。 因此,对该变量的索引的调用是不确定的。 那么,您要寻找的答案将不是$count = $this->shoppingBag[$this->productToShow][$prodName];

尝试:

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

$this->productToShow不是数组。 因此,将$prodName视为$prodName $this->shoppingBag特定项目的键,

我假设您希望$ count返回最后一个键,因为如果我们忽略了错误地将[$prodName]应用于$this->productToShow键,则$ count将返回购物袋中最后一个键的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM