Skip to content

Instantly share code, notes, and snippets.

@voku
Created June 4, 2014 10:35
Show Gist options
  • Save voku/82ec8dbf96df4bc18df0 to your computer and use it in GitHub Desktop.
Save voku/82ec8dbf96df4bc18df0 to your computer and use it in GitHub Desktop.
create and parse JSON-DATA via PHP - DEMO: http://ideone.com/bz6gjc
<?php
// create JSON data format
$json_data = array ('id'=>1,'name'=>"rolf",'country'=>'russia',"office"=>array("google","oracle"));
echo json_encode($json_data);
echo "\n\n";
// parse JSON data into PHP object
$json_string='{"id":1,"name":"rolf","country":"russia","office":["google","oracle"]} ';
$obj = json_decode($json_string);
//print the parsed data
echo $obj->name; //displays rolf
echo $obj->office[0]; //displays google
echo "\n\n";
// parse JSON data into PHP array
$json_string='{"id":1,"name":"rolf","country":"russia","office":["google","oracle"]} ';
$array = json_decode($json_string, true);
//print the parsed data
echo $array['name']; //displays rolf
echo $array['office'][0]; //displays google
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment