多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
最近开发微信公众号 需要判断借口返回的数据是否json字符串。 本想用`json_decode`数据根据结果是否`false`来判断是否成功,结果出现了期待意外的结果 ``` $data = 123; $data = "123"; $data = "abc"; $data = ["a"=>1]; var_dump(json_decode($data)); # 分别是 int(123); int(123); NULL; 告警: # json\_decode() expects parameter 1 to be string, array given ``` 猜测函数内部解码第一步是将参数1进行强转字符串   目前是用如下方法判断 ``` function isJson($string, &$decode_result=false){ $decode_result = json_decode($string); // $this->log($decode_result); return (json_last_error() == JSON_ERROR_NONE) && $decode_result && (is_object($decode_result) || is_array($decode_result)); } ```