convert response to json php code example

Example 1: php response json

<?PHP
$data = /** whatever you're serializing **/;
header('Content-Type: application/json');
echo json_encode($data);

Example 2: php json_decode

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

$person = json_decode($personJSON);

echo $person->name; // Johny Carson

Example 3: php json encode

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

Example 4: return json in php

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