⊗ppSpJnOTD 16 of 83 menu

Converting Objects from JSON to PHP

There are nuances when converting JSON objects. The fact is that they are converted not into PHP associative arrays, but into PHP objects.

Let's take a look. Suppose we have the following JSON:

<?php $json = '{ "a": 1, "b": 2, "c": 3 }'; ?>

Let's convert it to a PHP data structure:

<?php $data = json_decode($json); ?>

Let's check what we got:

<?php var_dump($data); // PHP object ?>

To output our values by keys, you need to access the properties of the resulting object:

<?php echo $data->a; // outputs 1 echo $data->b; // outputs 2 echo $data->c; // outputs 3 ?>

Convert the following JSON into a PHP structure:

<?php $json = '{ "user": { "name": "john", "surn": "smit" }, "city": "London" }'; ?>

Display the name, surname, and city.

Convert the following JSON into a PHP structure:

<?php $json = '{ "list1": ["value11", "value12", "value13"], "list2": ["value21", "value22", "value23"] }'; ?>
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline