PHP JSON PHP
- PHP Tutorial
- What is PHP
- Install PHP
- PHP Example
- PHP Echo
- PHP Print
- PHP Variables
- PHP $ and $$ Variables
- PHP Constants
- Magic Constants
- PHP Data Types
- PHP Operators
- PHP Comments
- PHP Boolean
- PHP is_bool() function
- PHP Integer
- PHP is_int() function
- PHP Float
- PHP is_float Function
- PHP Compound Types
- PHP Special Types
- PHP is_null() function
- PHP If Else
- PHP Switch
- PHP For Loop
- PHP While Loop
- PHP do while loop
- PHP Break
- PHP Functions
- PHP Parameterized Function
- PHP Call By Value
- PHP Call By Reference
- PHP Default Argument Values Function
- PHP Variable Length Argument Function
- PHP Recursive Function
- PHP Arrays
- PHP Indexed Array
- PHP Associative Array
- PHP Multidimensional Array
- PHP Array Functions
- PHP String
- PHP String Functions
- PHP Math functions
- PHP Form Handling | GET and POST method
- PHP Include File
- PHP Cookie
- PHP Session
- PHP File Handling
- PHP Open File
- PHP Read File
- PHP Write File
- PHP Append to File
- PHP Delete File
- PHP File Upload
- PHP Download File
- PHP Mail
- PHP MySQL Connect
- PHP MySQL Create Database
- PHP MySQL Create Table Example
- PHP MySQL Insert Record
- PHP Prepared Statements
- PHP MySQL Update Record
- PHP MySQL Delete Record
- PHP MySQL Select
- PHP MySQL Order By
- PHP Limit Data MySql
- PHP JSON
- PHP XML Parsers
- PHP SimpleXML Parser
- PHP SimpleXML - Get Node/Attribute Values
- PHP XML Expat Parser
- PHP XML DOM Parser
- PHP - AJAX intro
- MySQL CREATE VIEW
PHP JSON
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 variable (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.
- <?php
- $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.
- <?php
- $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.
- <?php
- $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)
}
["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.
- <?php
- $json2 = '{"firstName" : "Rahul", "lastName" : "Kumar", "email" : "rahul@gmail.com"}';
- var_dump(json_decode($json2, true));
- ?>
Output
array(3) {
["firstName"]=> string(5) "Rahul"
["lastName"]=> string(5) "Kumar"
["email"]=> string(15) "rahul@gmail.com"
}
["firstName"]=> string(5) "Rahul"
["lastName"]=> string(5) "Kumar"
["email"]=> string(15) "rahul@gmail.com"
}