⊗ppSpJnOTD 16 of 95 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 = '{ "ru": ["пн", "вт", "ср"], "en": ["mn", "tu", "wd"] }'; ?>

Display the Russian name for Wednesday.

byenru