JSON 在 php 网络编程中解码
JSON decode in php web programming
我在 php 使用 CURL 时遇到问题。我发出了一个 https 请求,我在多维数组中得到了响应。
[{
"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;
还有,如果你只是想把id
的值赋值给另一个变量,你可以直接对
...
$no = $a[0]->id;
echo $no
...
而不是 foreach 循环。
$books = json_decode($result, true);
foreach ($books as $book)
{
//$book carries info of books;
$id = $book['id'];
///... You can define other variables here
}
我在 php 使用 CURL 时遇到问题。我发出了一个 https 请求,我在多维数组中得到了响应。
[{
"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;
还有,如果你只是想把id
的值赋值给另一个变量,你可以直接对
...
$no = $a[0]->id;
echo $no
...
而不是 foreach 循环。
$books = json_decode($result, true);
foreach ($books as $book)
{
//$book carries info of books;
$id = $book['id'];
///... You can define other variables here
}