php api return json code example
Example 1: php change header to json
<?PHP
$data = ;
header('Content-Type: application/json');
echo json_encode($data);
Example 2: php return json data
header('Content-Type: application/json');
$colors = array("red","blue","green");
echo json_encode($colors);
Example 3: php json_encode header detail
$array = array();
$array['Name'] = 'Alex';
$array['Age'] = 37;
$array['Admin'] = TRUE;
$array['Contact'] = array
(
'Site' => "alexwebdevelop.com",
'Phone' => 123456789,
'Address' => NULL
);
$array['Tags'] = array('php', 'web', 'dev');
$json = json_encode($array, JSON_PRETTY_PRINT);
echo '<pre>';
echo $json;
echo '</pre>';
Example 4: web api return json example in php
$option = $_GET['option'];
if ( $option == 1 ) {
$data = [ 'a', 'b', 'c' ];
} else {
$data = [ 'name' => 'God', 'age' => -1 ];
}
header('Content-type: application/json');
echo json_encode( $data );