我有一个函数,它发送查询并接收以下格式的数组:
{
"id1" : 312293.23451244,
"id2" : 6.03937464,
"id3" : 1
}
使用PHP,我怎么能返回"id1"的值?
谢谢!
$data = '{
"id1" : 312293.23451244,
"id2" : 6.03937464,
"id3" : 1
}';
$json = json_decode($data, true);
return $json['id1'];
使用json_decode
$str = '{
"id1" : 312293.23451244,
"id2" : 6.03937464,
"id3" : 1
}';
$json = json_decode($str, true);
echo $json['id1'];
您收到的作为您的查询响应的一部分的上述数据在
json格式
所以要从json内容访问数据,你需要使用php库中定义的json_decode函数。
。
$result='{
"id1" : 312293.23451244,
"id2" : 6.03937464,
"id3" : 1
}';
$array=json_decode($result,true);
echo $array['id1'];
它接受json字符串作为第一个参数,第二个参数指定为true时,将返回一个更方便遍历的关联数组。
有关json_decode函数的更多详细信息,请参考以下url
中提到的文档http://php.net/manual/en/function.json-decode.php