⊗ppSpJnFD 14 of 95 menu

Data Conversion to JSON in PHP

Using the function json_encode you can automatically convert data in PHP format to JSON format. Let's look at an example. Suppose we have the following array:

<?php $data = [1, 2, 3]; ?>

Let's convert it to JSON:

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

Let's check the result:

<?php var_dump($json); // outputs '[1,2,3]' ?>

Convert the following PHP structure into JSON:

<?php $data = [ 'x' => 1, 'y' => 2, 'z' => 3, ]; ?>
byenru