xxxxxxxxxx
$arr = json_decode($json, true);
// Loop through the associative array
foreach($arr as $key=>$value){
echo $key . " => " . $value . "<br>";
}
xxxxxxxxxx
<?php
$jsonurl = "https://reqres.in/api/users/2";
$json = file_get_contents($jsonurl);
$jsonDecode = json_decode($json, true);
echo $jsonDecode['data']['email'];
?>
xxxxxxxxxx
//PHP File
<?php
$data = /** whatever your data are **/;
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data);
//In JS File
//JQUERY AJAX
$.ajax({
url: "path/to_php_file.php",
dataType: "json",
type: "GET",
data: {datax : datax },
xxxxxxxxxx
header('Content-Type: application/json');
$colors = array("red","blue","green");
echo json_encode($colors);
xxxxxxxxxx
$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
echo $data;
xxxxxxxxxx
function isJson($str) {
json_decode($str);
return json_last_error() === JSON_ERROR_NONE;
}
xxxxxxxxxx
function isJson($str) {
json_decode($str);
return json_last_error() === JSON_ERROR_NONE;
}
xxxxxxxxxx
// json object.
$contents = '{"firstName":"John", "lastName":"Doe"}';
// Option 1: through the use of an array.
$jsonArray = json_decode($contents,true);
$key = "firstName";
$firstName = $jsonArray[$key];
// Option 2: through the use of an object.
$jsonObject = json_decode($contents);
$key = "firstName";
$firstName = $jsonArray->$key;