There is an array $aa and the data in it is like this: [{goodid:270,num:1},{goodid:277,num:5}] I want to extract the values in goodid: 270 and 277. Write? Tried: foreach((array)$aa as $v){echo $v['goodid'];]} error.... I am a primary school student who has just started, please help and guide me, thank you very much?
沐雨轩2021-01-30 10:58:42
First determine {goodid:270,num:1}. This is not a correct type in php, but it is correct in js.
$arr = [ "{'goodid':270, 'num':1}", "{'goodid':277,'num':5}",];$newarray = [];foreach ($arr as $value){ $value = json_decode($value,true ); $newarray[] = $value['goodid'];}print_r($newarray);
灭绝师太2021-01-27 09:32:10
First of all, arrays and objects in php are completely separated, you can solve it on the front end
var arr = [{ goodid: 270, num: 1 }, { goodid: 277, num: 5 }]; let newArr = []; for (let index in arr) { newArr[index] = arr[index]['goodid']; }; console.log(newArr);
夜夜**2021-01-26 22:17:46
Masters passing by, please help me take a look~Thank you very much? ? ?