Accessing associative arrays in PHP

百般思念 提交于 2019-12-18 04:48:11

问题


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

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

回答1:


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"];
}



回答2:


Do you want this ?

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


来源:https://stackoverflow.com/questions/3842111/accessing-associative-arrays-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!