header('Content-Type: application/json');
$colors = array("red","blue","green");
echo json_encode($colors);
$character = json_decode($data);
echo $character->name;
$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
echo $data;
<?php
$json = '[{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"456","email":"b@a.com"},{"name":"zzz","phone":"678","email":"c@a.com"}]';
$json_decoded = json_decode($json);
echo '<table>';
foreach($json_decoded as $result){
echo '<tr>';
echo '<td>'.$result->name.'</td>';
echo '<td>'.$result->phone.'</td>';
echo '<td>'.$result->email.'</td>';
echo '</tr>';
}
echo '</table>';
?>
$data = json_decode($json);
//code igniter
$query="qry";
$query = $this->db->query($query);
$res=$query->result();
return json_encode($res);
$json = '
{
"type": "donut",
"name": "Cake",
"toppings": [
{ "id": "5002", "type": "Glazed" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5004", "type": "Maple" }
]
}';
$yummy = json_decode($json);
print_r($yummy);
$arr = json_decode($json, true);
// Loop through the associative array
foreach($arr as $key=>$value){
echo $key . " => " . $value . "<br>";
}
<?php
$data = /** whatever you're serializing **/;
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data);