TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

PHP JSON

PHP JSON example for beginners and professionals with examples of JSON with java, .net, php, python, xml, jquery, ruby, c#, perl, jackson. Learn PHP JSON example with array, object, schema, encode, decode, file, date etc.

<< Back to PHP

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.

 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.

 'Rahul', 'lastName' => 'Kumar', 'email' => 'rahul@gmail.com');  
echo json_encode($arr2);
?>

Output

{"firstName":"Rahul","lastName":"Kumar","email":"[email protected]"}

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.


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.


Output

array(3) { 
["firstName"]=> string(5) "Rahul" 
["lastName"]=> string(5) "Kumar" 
["email"]=> string(15) "[email protected]" 
}

Next TopicJava JSON Example




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf