JSON, short for JavaScript Object Notation, is a lightweight text format designed for storing and exchanging data. Its text-based
structure allows for easy transmission between a client and server, making it highly compatible with numerous programming languages for efficient data representation and manipulation.
PHP and JSON
PHP offers a variety of built-in functions to handle JSON data.
Let's start by exploring two essential functions.
json_encode()
json_decode()
json_encode()
The json_encode()
function is used to convert a value into JSON format.
Example: Convert an associative array into a JSON object.
<?php
$fruits = array("Apple" => "Red", "Banana" => "Yellow", "Grape" => "Purple");
echo json_encode($fruits);
/*
Output:
{"Apple":"Red","Banana":"Yellow","Grape":"Purple"}
*/
?>
<?php
$fruits = array("Apple", "Banana", "Grape");
echo json_encode($fruits);
/*
Output:
["Apple","Banana","Grape"]
*/
?>
json_decode()
The json_decode()
function is used to transform a JSON string into a PHP object or an associative array.
Example: Decode JSON data into a PHP object.
<?php
$jsonobj = '{"Apple":"Red","Banana":"Yellow","Grape":"Purple"}';
var_dump(json_decode($jsonobj));
/*
Output:
object(stdClass)#1 (3) {
["Apple"]=> string(3) "Red"
["Banana"]=> string(6) "Yellow"
["Grape"]=> string(6) "Purple"
}
*/
?>
By default, the json_decode()
function converts JSON data into a PHP object. However, if you pass true
as the second parameter, it will decode JSON objects into associative arrays.
Example: Decode JSON data into a PHP associative array.
<?php
$jsonobj = '{"Apple":"Red","Banana":"Yellow","Grape":"Purple"}';
var_dump(json_decode($jsonobj, true));
/*
Output:
array(3) {
["Apple"]=> string(3) "Red"
["Banana"]=> string(6) "Yellow"
["Grape"]=> string(6) "Purple"
}
*/
?>
Accessing the Decoded Values
The following examples demonstrate how to access decoded values from both a PHP object and an associative array.
Example: Access values from a PHP object.
<?php
$jsonobj = '{"Apple":"Red","Banana":"Yellow","Grape":"Purple"}';
$obj = json_decode($jsonobj);
echo $obj->Apple . ', ';
echo $obj->Banana . ', ';
echo $obj->Grape;
/*
Output:
Red, Yellow, Purple
*/
?>
Example: Access values from a PHP associative array.
<?php
$jsonobj = '{"Apple":"Red","Banana":"Yellow","Grape":"Purple"}';
$arr = json_decode($jsonobj, true);
echo $arr["Apple"];
echo $arr["Banana"];
echo $arr["Grape"];
/*
Output:
RedYellowPurple
*/
?>
Looping Through the Values
You can also loop through the values using a foreach()
loop.
Example: Shows how to iterate through the values of a PHP object.
<?php
$jsonobj = '{"Apple":"Red","Banana":"Yellow","Grape":"Purple"}';
$obj = json_decode($jsonobj);
foreach($obj as $key => $value) {
echo $key . " => " . $value . "<br>";
}
/*
Output:
Apple => Red
Banana => Yellow
Grape => Purple
*/
?>
Example: Illustrates how to loop through the values of a PHP associative array.
<?php
$jsonobj = '{"Apple":"Red","Banana":"Yellow","Grape":"Purple"}';
$arr = json_decode($jsonobj, true);
foreach($arr as $key => $value) {
echo $key . " => " . $value . "<br>";
}
/*
Output:
Apple => Red
Banana => Yellow
Grape => Purple
*/
?>