About
In this code snippet, we’ll learn how to work with JSON in PHP.
You can use json_encode() to serialize and json_decode() to deserialize JSON. json_decode() also takes a second parameter(true/false) where you can specify if you want the JSON to be deserialized to an associative array or to an object.
In this post, you can see a practical example of JSON serialization/deserialization.
Let’s see the example code below.
Code:
<?php
//Array/Object
$data = [
"people" => [
[ "id" => "1", "name" => "Bob"],
[ "id" => "2", "name" => "Alice"]
]
];
//Serialize.
$jsonString = json_encode($data);
//Deserialize.
$decodedData = json_decode($jsonString, true);





