I have a json array (dataArray) sent from js to php using JSON.stringuify.
I'm getting the output after using json_decode that object like this:
$resp = json_decode($dataArray,true);
error_log($resp);
Log:
[{"id":"0","name":"JOAO"},{"id":"1","name":"TONI"},{"id":"2","name":"ANA"}]
I'm trying to get the id and name values from each element in that array.
Using foreach returns an error since the object is not considered as an array... So I tried casting it to an array and then use it in the foreach, and it's size is still 1:
$arr = (array) $res;
error_log(sizeof($arr[0]));
foreach ($arr[0] as $dt) {
error_log($dt);
}
This returns the "full sized 1 array"
[{"id":"0","name":"JOAO"},{"id":"1","name":"TONI"},{"id":"2","name":"ANA"}]
and if I try error_log($dt->id) I don't get any results...
I'm not understanding why this is not being considered as an array.
error_log(which expects a string) without any issues, it sounds like your data might be double-encoded? Tryjson_decode(json_decode($dataArray),true);var_dump($resp)show?$dataArraycontains.