我正在尝试提取从google api
返回的特定值<?php
$url='http://maps.googleapis.com/maps/api/distancematrix/json?origins=19.1629924,72.83930190&destinations=19.1802370,72.8554149&sensor=false';
$contents = file_get_contents($url);
$contents = utf8_encode($contents);
$array=json_decode($contents);
echo $array['rows']['elements']['distance']['value'];
?>
我预计会返回4238的价值。相反,我得到一个错误:
`PHP Fatal error: Cannot use object of type stdClass as array in test9.php on` line 7
答案 0 :(得分:1)
正确的解决方案是:
echo $array->rows[0]->elements[0]->distance->value;
执行以下操作以查看json:
<?php
$url='http://maps.googleapis.com/maps/api/distancematrix/json?origins=19.1629924,72.83930190&destinations=19.1802370,72.8554149&sensor=false';
$contents = file_get_contents($url);
$contents = utf8_encode($contents);
echo "<pre>";
echo $contents;
echo "\n";
echo "\n";
$array=json_decode($contents);
echo $array->rows[0]->elements[0]->distance->value;
?>
你试过node.js吗?
答案 1 :(得分:0)
以下代码解决了这个问题:
$array=json_decode($contents, true);
echo $array['rows'][0]['elements'][0]['distance']['value'];