
哎,现在的网上瞎 J8 扯的人真是太多了,全 TM 一片抄!
网上一堆人说的 json_last_error()
,可以判断 json_decode
后的结果
我也没多想,试了一下:
json_decode('123');
var_dump(json_last_error());
然后可笑的去 json_last_error() == JSON_ERROR_NONE
,返回的是 true
吧?
那还怎么比较…
写了一个大家可参考:
/**
* @param string $data Json 字符串
* @param bool $assoc 是否返回关联数组
* @return bool|array 成功返回转换后的数组,否则返回 false
*/
function isJson($data = '', $assoc = false) {
$data = json_decode($data, $assoc);
if (is_array($data) && !empty(current($data))) {
return $data;
}
return false;
}
文章来源: PHP 判断字符串是否为 Json 格式
人吐槽 | 人点赞 |
发表评论