1$myArr = array("apple", "banana", "mango", "jackfruit");
2
3$toJSON = json_encode($myArr);
4
5echo $toJSON;
1<?php
2 // JSON string
3 $someJSON = '[{"name":"Jonathan Suh","gender":"male"},{"name":"William Philbin","gender":"male"},{"name":"Allison McKinnery","gender":"female"}]';
4
5 // Convert JSON string to Array
6 $someArray = json_decode($someJSON, true);
7 print_r($someArray); // Dump all data of the Array
8 echo $someArray[0]["name"]; // Access Array data
9
10 // Convert JSON string to Object
11 $someObject = json_decode($someJSON);
12 print_r($someObject); // Dump all data of the Object
13 echo $someObject[0]->name; // Access Object data
14?>
15
1//Json Encode
2
3$person = array(
4 "name" => "KINGASV",
5 "title" => "CTO"
6);
7$personJSON=json_encode($person);//returns JSON string
8
9//Json Decode
10
11$personJSON = '{"name":"KINGASV","title":"CTO"}';
12
13$person = json_decode($personJSON);
14
15echo $person->name; // KINGASV
16
1$personJSON = '{"name":"Johny Carson","title":"CTO"}';
2
3$person = json_decode($personJSON);
4
5echo $person->name; // Johny Carson
1//2 ways
2 //this is for string from $_REQUEST,$_POST to array
3$jsonText = $_REQUEST['myJSON'];
4$decodedText = html_entity_decode($jsonText);
5$myArray = json_decode($decodedText, true);
6
7//this is for json to array
8$assosiative_array = json_decode(json_encode($jsonText),true);
1$person = array(
2 "name" => "Johny Carson",
3 "title" => "CTO"
4);
5$personJSON=json_encode($person);//returns JSON string