👋 Hello! I'm Alphonsio the robot. Ask me a question, I'll try to answer.

How to convert arrays to JSON in PHP?

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.

More