how to get data from json array in php | json in php | json decode and encode in php
This chapter covevironment to start our programming with PHP for JSON.rs how to encode and decode JSON objects using the PHP programming language. Let’s start with preparing the en
Environment
As of PHP 5.2.0, the JSON extension is bundled and compiled into PHP by default.
JSON Functions
Function Libraries
json_encode Returns the JSON representation of a value.
json_decode Decodes a JSON string.
json_last_error Returns the last error occurred.
Encoding JSON in PHP (json_encode)
PHP json_encode() function is used for encoding JSON in PHP. This function returns the JSON representation of a value on success or FALSE on failure.
Related :
- Ajax CRUD [insert, Update, Delete] with PHP and MySQL database 2021
- Top 50 JavaScript Interview Questions Must Prepare
- php laravel developer salary in india For Freshers & Ex.
Syntax
string json_encode ( $value [, $options = 0 ] )
Parameters
value − The value being encoded. This function only works with UTF-8 encoded data.
options − This optional value is a bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT.
Example
The following example shows how to convert an array into JSON with PHP −
$arr = array(‘a’ => 1, ‘b’ => 2, ‘c’ => 3, ‘d’ => 4, ‘e’ => 5);
echo json_encode($arr);
result –
{“a”:1,”b”:2,”c”:3,”d”:4,”e”:5}
The following example shows how the PHP objects can be converted into JSON −
class Emp {
public $name = “”;
public $hobbies = “”;
public $birthdate = “”;
}
$e = new Emp();
$e->name = “sachin”;
$e->hobbies = “sports”;
$e->birthdate = date(‘m/d/Y h:i:s a’, “8/5/1974 12:20:03 p”);
$e->birthdate = date(‘m/d/Y h:i:s a’, strtotime(“8/5/1974 12:20:03”));
echo json_encode($e);
While executing, this will produce the following result −
{“name”:”sachin”,”hobbies”:”sports”,”birthdate”:”08/05/1974 12:20:03 pm”}
Decoding JSON in PHP (json_decode)
PHP json_decode() function is used for decoding JSON in PHP. This function returns the value decoded from json to appropriate PHP type.
Syntax
mixed json_decode ($json [,$assoc = false [, $depth = 512 [, $options = 0 ]]])
Paramaters
json_string − It is an encoded string which must be UTF-8 encoded data.
assoc − It is a boolean type parameter, when set to TRUE, returned objects will be converted into associative arrays.
depth − It is an integer type parameter that specifies recursion depth
options − It is an integer type bitmask of JSON decode, JSON_BIGINT_AS_STRING is supported.
Example
The following example shows how PHP can be used to decode JSON objects −
$json = ‘{“a”:1,”b”:2,”c”:3,”d”:4,”e”:5}’;
var_dump(json_decode($json));
var_dump(json_decode($json, true));
While executing, it will produce the following result −
object(stdClass)#1 (5) {
[“a”] => int(1)
[“b”] => int(2)
[“c”] => int(3)
[“d”] => int(4)
[“e”] => int(5)
}
array(5) {
[“a”] => int(1)
[“b”] => int(2)
[“c”] => int(3)
[“d”] => int(4)
[“e”] => int(5)
}
PHP JSON
PHP allows us to encode and decode JSON by the help of json_encode() and json_decode functions.
1) PHP json_encode
The json_encode() function returns the JSON representation of a value. In other words, it converts PHP variables (containing array) into JSON.
Syntax:
string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )
PHP json_encode example 1
Let’s see the example to encode JSON.
$arr = array(‘a’ => 1, ‘b’ => 2, ‘c’ => 3, ‘d’ => 4, ‘e’ => 5);
echo json_encode($arr);
Output
{“a”:1,”b”:2,”c”:3,”d”:4,”e”:5}
PHP json_encode example 2
Let’s see the example to encode JSON.
$arr2 = array(‘firstName’ => ‘Rahul’, ‘lastName’ => ‘Kumar’, ’email’ => ‘rahul@gmail.com’);
echo json_encode($arr2);
Output
{“firstName”:”Rahul”,”lastName”:”Kumar”,”email”:”rahul@gmail.com”}
2) PHP json_decode
The json_decode() function decodes the JSON string. In other words, it converts JSON string into a PHP variable.
Syntax:
mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
PHP json_decode example 1
Let’s see the example to decode JSON string.
$json = ‘{“a”:1,”b”:2,”c”:3,”d”:4,”e”:5}’;
var_dump(json_decode($json, true));//true means returned object will be converted into associative array
Output
array(5) {
[“a”] => int(1)
[“b”] => int(2)
[“c”] => int(3)
[“d”] => int(4)
[“e”] => int(5)
}
PHP json_decode example 2
Let’s see the example to decode JSON string.
$json2 = ‘{“name” : “Vikram”, “lastName” : “Kuldeep”, “email” : “Vikramkuldeep830@gmail.com”}’;
var_dump(json_decode($json2, true));
Output
array(3) {
[“firstName”]=> string(5) “vikram”
[“lastName”]=> string(5) “Kuldeep”
[“email”]=> string(15) “vikramkuldeep830@gmail.com”
}
how to get data from JSON array in PHP using for loop,
how to get data from JSON objects in PHP,
how to get data from JSON array in javascript,
how to get data from JSON array in laravel,
PHP parse JSON array,
PHP get JSON data from API,
PHP get JSON data from the post,
PHP get JSON data from URL.
Related searches: get data from json array