在 PHP 中访问关联数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3842111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:17:40  来源:igfitidea点击:

Accessing associative arrays in PHP

phparraysassociative

提问by slao

I want to access the index 'memo' in the associative array in PHP below

我想在下面的 PHP 中访问关联数组中的索引“备忘录”

$variables["thelistitems"];
print_r($variables["thelistitems"]);

Output

输出

Array
(
    [0] => Array
    (
        [productid] => prod:c6dbdd62-dc13-6421-5a94-c8cd871a59d3 
        [memo] => dummy 
        [taxable] => 0 
        [unitweight] => 0 
        [unitcost] => 450.02 
        [unitprice] => 445.02 
        [quantity] => 1
    )
) 

回答by villecoder

What you essentially have is an array of associative arrays. So to access the first memo, it's

您本质上拥有的是一组关联数组。所以要访问第一个备忘录,它是

 $variables["thelistitems"][0]["memo"]

To access each memo, you'd do something like this

要访问每个备忘录,您可以执行以下操作

foreach($variables["thelistitems"] as $listitem) {
    $memo = $listitem["memo"];
}

回答by Lo?c Février

Do you want this ?

你想要这个吗 ?

$variables["thelistitems"][0]['memo']