how to return json object in php code example
Example 1: json php
$person = array(
"name" => "KINGASV",
"title" => "CTO"
);
$personJSON=json_encode($person);
$personJSON = '{"name":"KINGASV","title":"CTO"}';
$person = json_decode($personJSON);
echo $person->name;
Example 2: php access json object
<?php
$someJSON = '[{"name":"Jonathan Suh","gender":"male"},{"name":"William Philbin","gender":"male"},{"name":"Allison McKinnery","gender":"female"}]';
$someObject = json_decode($someJSON);
echo $someObject[0]->name;
?>
Example 3: convert text file to json php
<?php
header('Content-type: application/json');
echo json_encode( explode("\r\n",file_get_contents('data.txt')) );
?>
Example 4: return json in php
$query="qry";
$query = $this->db->query($query);
$res=$query->result();
return json_encode($res);