To Create and Parse JSON data in PHP :
We can use json_encode() function of PHP to create JSON data format. Below is the PHP snippet to create the JSON data format of given array of PHP :

Example :

 <?php
                               
$json_data = array(
	        "id" => 1,
	     	"name" => "Rahul Yadav",
	     	"country" => "India",
	     	"email" => "admin@codephponline.com",
	     	"frameworks" => array(
		                  "Codeigniter",
		                  "Laravel"
		                  )
             );
$json_string = json_encode($json_data);
echo $json_string;

 ?>
 
 
 /*

 Output :

 {"id":1,"name":"Rahul Yadav","country":"India","email":"admin@codephponline.com","frameworks":["Codeigniter","Laravel"]}

 */

  
We can use json_decode() function of PHP to parse the JSON data. Below code will parse the JSON data into PHP arrays.

Example :

 <?php
                               
$json_string='{"id":1,"name":"Rahul Yadav","country":"India","email":"admin@codephponline.com","frameworks":["Codeigniter","Laravel"]}';
$obj=json_decode($json_string);
//print the parsed data
echo $obj->id."
"; // displays 1 echo $obj->name."
"; // displays Rahul Yadav echo $obj->country."
"; // displays India echo $obj->email."
"; //displays admin@codephponline.com echo $obj->frameworks[0]."
"; // displays Codeigniter echo $obj->frameworks[1]."
"; // displays Laravel ?> /* Output : 1 Rahul Yadav India admin@codephponline.com Codeigniter Laravel */
About Me
Rahul Yadav

Rahul Yadav

Technical Architect

Kuala Lumpur, Malaysia

Connect Me:     

I'm a Full Stack Web Developer working in a MNC and passionate of developing modern web and mobile applications.

I have designed and developed CodephpOnline & CodephpOnline Wiki platform to expatiate my coding and technology learning experiences.

In my leisure time, I write technical articles on web development such as PHP, Laravel, CodeIgniter, Mediawiki, Linux, Angular, Ionic, ReactJS, NodeJS, AJAX, jQuery, Cloud and more.

Tag Cloud