php read json data code example

Example 1: json php

//Json Encode

$person = array( 
    "name" => "KINGASV", 
    "title" => "CTO"
); 
$personJSON=json_encode($person);//returns JSON string

//Json Decode

$personJSON = '{"name":"KINGASV","title":"CTO"}';

$person = json_decode($personJSON);

echo $person->name; // KINGASV

Example 2: php json_decode

$personJSON = '{"name":"Johny Carson","title":"CTO"}';

$person = json_decode($personJSON);

echo $person->name; // Johny Carson

Example 3: php decode json file

$json = json_decode(file_get_contents('/path/to/your/file.json'));

Example 4: php return json

header('Content-type: application/json');
echo json_encode($array);

Example 5: parse json phph

echo json_decode("[1,2,3]")[0];

Example 6: return json in php

//code igniter
$query="qry";
$query = $this->db->query($query);
$res=$query->result();
return json_encode($res);

Tags:

Php Example