php web编程中的JSON解码
我在使用CURL的php中遇到了一个问题。我发出了一个https请求,得到了多维数组中的响应php web编程中的JSON解码,php,arrays,json,curl,response,Php,Arrays,Json,Curl,Response,我在使用CURL的php中遇到了一个问题。我发出了一个https请求,得到了多维数组中的响应 [{ "id":"22622", "name":"", "email":"ffv7678@gmail.com", "mobileno":"", "birth_dt":"", "marital_status":"", "gender":"", "educationid":"0", "occupationid":"0", "industryid":"0", "incomeid":"
[{
"id":"22622",
"name":"",
"email":"ffv7678@gmail.com",
"mobileno":"",
"birth_dt":"",
"marital_status":"",
"gender":"",
"educationid":"0",
"occupationid":"0",
"industryid":"0",
"incomeid":"",
"city":"0",
"state":"0",
"country":"0",
"postcode":"",
"deviceid":"805086099499488",
"regid":"",
"device_type":null,
"userstatus":"0",
"refcode":"D1219C92",
"new_user":"0",
"device_token":""
}]
现在我想解码它并将“id”的值保存在一个变量中
$result=curl_exec($ch);
$books = json_decode($result, true);
echo ($books[0]['id']);
我尝试了上面的代码,但失败了。为此需要使用
foreach()
循环。foreach ($a[0] as $key => $value) {
echo $key."<br>".$value;
if($key == 'id'){
$id = $value;
}
}
echo $id;
而不是foreach循环。您的错误是什么?感谢您的回复!但还是有问题。@SivaKrishnaSamireddi检查我的答案!如何将“id”的值分配给某个变量,比如$number?请提出不同的问题。这将违反堆栈协议,无法解码吗?我是初学者,如果我的问题是错误的,对不起!json_decode($result)将json对象解码为数组()。解码完成了。index.php中的文件在第124行意外结束。这是什么意思?
$books = json_decode($result, true);
foreach ($books as $book)
{
//$book carries info of books;
$id = $book['id'];
///... You can define other variables here
}
$books = json_decode($result, true);
foreach ($books as $book)
{
//$book carries info of books;
$id = $book['id'];
///... You can define other variables here
}