PHP receives this via AJAX in JSON format (let's call it $json_string):
[{"pid":"284","qty":"1","sn":"12"},{"pid":"284","qty":"1","sn":"23"},{"pid":"276","qty":"1","sn":"34"},{"pid":"276","qty":"1","sn":"45"},{"pid":"276","qty":"1","sn":"56"},{"pid":"281","qty":"1","sn":"57"},{"pid":"281","qty":"1","sn":"67"},{"pid":"281","qty":"1","sn":"78"}]
I wish to loop through the arrays, like so:
$out = '<table>';
$arr = json_decode($json_string);
foreach ($arr AS $row){
$out .= '<tr><td>'.$row['pid'].'</td><td>'.$row['qty'].'</td><td>'.$row['sn'].'</td></tr>';
}
$out .= '</table>';
I am getting an error: Fatal error: Cannot use object of type stdClass as array
$arr = json_decode($json_string, true)results in an associative array:$pid = $arr['pid'];-- whereas just$arr = json_decode($json_string)results in a simple array:$pid = $arr[0];Depends on the data, not just the desired output. Above is assoc data.