Question #83   Submitted by Answiki on 06/17/2020 at 01:11:09 PM UTC

How to convert PHP arrays to JSON?

Answer   Submitted by Answiki on 08/18/2021 at 12:55:53 PM UTC

The PHP function json_encode() returns a string containing the first parameter converted into JSON.


$myArray = array('one' => 1, 'two' => 2, 'three' => 3);
$json = json_encode($myArray);

// Expected output : "{"one":1,"two":2,"three":3}"
echo $json;

If the conversion fails, json_encode() returns false.

4 events in history
Answer by Answiki on 08/18/2021 at 12:55:53 PM

The PHP function json_encode() returns a string containing the first parameter converted into JSON.


$myArray = array('one' => 1, 'two' => 2, 'three' => 3);
$json = json_encode($myArray);

// Expected output : "{"one":1,"two":2,"three":3}"
echo $json;

If the conversion fails, json_encode() returns false.

Question by Answiki 06/17/2020 at 01:16:57 PM
How to convert arrays to JSON in PHP?
Answer by Answiki on 06/17/2020 at 01:16:41 PM

The PHP function json_encode() returns a string containing the first parameter converted into JSON.


$myArray = array('one' => 1, 'two' => 2, 'three' => 3);
$json = json_encode($myArray);

// Expected output : "{"one":1,"two":2,"three":3}"
echo $json;

If the conversion failed, json_encode() returns false.

Question by Answiki 06/17/2020 at 01:11:09 PM
How to convert PHP arrays to JSON?
# ID Query URL Count

Icons proudly provided by Friconix.