PHP json_decode()输出:对象与数组?
发布时间:2020-12-13 16:50:14 所属栏目:PHP教程 来源:网络整理
导读:对于函数json_decode(),有2个输出选项,JSON Object或Array. $obj = json_decode($json_string,false); 要么 $array = json_decode($json_string,true); 哪种类型表现更好? 解决方法 对于函数json_decode(),人们可能会在将结果输出为Object或Associate Array
对于函数json_decode(),有2个输出选项,JSON Object或Array.
$obj = json_decode($json_string,false); 要么 $array = json_decode($json_string,true); 哪种类型表现更好? 解决方法
对于函数json_decode(),人们可能会在将结果输出为Object或Associate Array之间挣扎.在这里我进行了基准测试.
使用的代码(其中$json_string是Google Maps V3地理编码API的JSON输出): // object $start_time = microtime(true); $json = json_decode($json_string,false); echo '(' . $json->results[0]->geometry->location->lat . ',' . $json->results[0]->geometry->location->lng . ')' . PHP_EOL; $end_time = microtime(true); echo 'JSON Object: ' . round($end_time - $start_time,6) . 's' . PHP_EOL; // array $start_time = microtime(true); $json = json_decode($json_string,true); echo '(' . $json['results'][0]['geometry']['location']['lat'] . ',' . $json['results'][0]['geometry']['location']['lng'] . ')' . PHP_EOL; $end_time = microtime(true); echo 'JSON Array: ' . round($end_time - $start_time,6) . 's' . PHP_EOL; 我发现Array比Object快30%~50%. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |