我想从PHP codeigniter 4中的Json数组读取所有数据[重复]

eiee3dmh  于 2022-12-01  发布在  PHP
关注(0)|答案(1)|浏览(131)

此问题在此处已有答案

How to extract and access data from JSON with PHP?(1个答案)
How can I access an array/object?(6个答案)
昨天关门了。
我已经使用jquery生成了html表数据到Json数组,它为我工作,我得到了下面的数组,我需要php代码来迭代php codeigniter 4控制器中这个数组的每个值。

array(1){
  [
    0
  ]=>string(966)"[{"ID":"\n2","Name":"\nCP","Price":"\n350.20"},{"ID":"\n3","Name":"\nLFT","Price":"\n700.10"},{"ID":"\n4","Name":"\nRFT","Price":"\n200"},{"ID":"\n5","Name":"\nurinetest","Price":"\n1000"}]"
}
fjaof16o

fjaof16o1#

这是一个包含json数组的php数组,如果你确定你在数组中只有一个元素,你可以这样做:

$array = json_decode($var[0]); // $var holds the data you mentioned above
foreach ($array as $item) {
    //
}

否则:

foreach ($var as $array) { // $var holds the data you mentioned above
    $array = json_decode($array);
    foreach ($array as $item) {
        //
    }
}

相关问题