Converting Between JSON and Arrays in PHP


Easily convert between JSON strings and PHP arrays using json_encode() and json_decode().

Source Code

// Convert array to JSON
$array = ['name' => 'John', 'age' => 30, 'city' => 'New York'];
$json = json_encode($array);
echo $json; // Outputs: {"name":"John","age":30,"city":"New York"}

// Convert JSON to array
$decodedArray = json_decode($json, true);
print_r($decodedArray); // Outputs the original array
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments