Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JSON Decode While/Foreach

I have a JSON data saved in the database. I want to put this data in a while loop and get everything in the line, but I can’t. How can I do it?

[{"count":33226,"info":"","name":"cash","slot":1,"type":"item"},{"count":1,"info":{"telno":"0662052408","isim":"Bob Brc","aitlik":"steam:1100001179c1b7d","durum":"kilitli"},"name":"phone","slot":2,"type":"item"},{"count":1,"info":{"uniqueId":"3_1","keyData":"z6VAhfL3ppApIXR"},"name":"motel_key","slot":3,"type":"item"},{"count":1,"info":[],"name":"ballasbandana","slot":10,"type":"item"}]
$inventory = $karakter['inventory'];
$inventoryjson = json_decode($inventory, true); 
$items = $inventoryjson['name'];

The way it is in my database above, there are more than one item here, but I could not separate them and put them in a loop. The code field at the bottom is also the PHP field.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

What you get from this json is a multidimensional array, which means the $items = $inventoryjson['name']; will produce an error.

You could use a foreach loop to get the correct info, like so:

$inventory = $karakter['inventory'];
$items = json_decode($inventory, true);

foreach ( $items as $item ) {
    $name = $item['name'];
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading