【发布时间】:2020-01-12 04:20:36
【问题描述】:
我正在尝试在 php 中循环遍历 JSON 数据。
array:2 [
"cart" => array:3 [
0 => array:4 [
"id" => 3
"name" => "ying"
"price" => "4000"
]
1 => array:4 [
"id" => 2
"name" => "yang"
"price" => "4000"
]
2 => array:4 [
"id" => 4
"name" => "foo"
"price" => "5000"
]
]
"total" => 13000
]
我使用了 json_decode 函数和对数据的 foreach。
foreach (json_decode($arr) as $item) {
$item['name'];
}
我希望能够获取每个“购物车”项目和单个“总”数据,但是当我尝试调用诸如 $item['name'] 之类的东西时,我不断收到非法偏移错误
【问题讨论】:
-
数组中的数组。你没有考虑到这一点。