Kaltsoon Kaltsoon - 22 days ago 7
JSON Question

How to loop through this json decoded data in PHP?

I've have this list of products in JSON that needs to be decoded:

"[{"productId":"epIJp9","name":"Product A","amount":"5","identifier":"242"},{"productId":"a93fHL","name":"Product B","amount":"2","identifier":"985"}]"


After I decode it in PHP with
json_decode()
, I have no idea what kind of structure the output is. I assumed that it would be an array, but after I ask for
count()
it says its "0". How can I loop through this data so that I get the attributes of each product on the list.

Thanks!

Answer

To convert json to an array use

 json_decode($json, true);