json_encode()返回null,因为数组有大量记录

时间:2015-06-12 06:58:53

标签: php arrays json

我已使用json_encode($response)列出目录中的所有文件。由于它在$response数组中有近8000条记录,当它显示在页面中时,它会返回null

我有关于这个问题的研究。到目前为止,我找不到任何解决方案。

这是我的代码:

$response['content'] = Files::$output;
echo json_encode($response);
Files::$output` /* this will return the list of files in the html format. */

当我尝试echo json_last_error_msg();时,它会返回:

  

格式错误的UTF-8字符,可能编码错误

1 个答案:

答案 0 :(得分:0)

JSON只能在通过json_encode对其进行编码时接受有效的UTF字符。

可能的是,您输入格式错误(正如您的错误消息所示),这很可能是Windows代码页的某些变体。

要确保您的数组没有无效的JSON数据,您可以在数据库层转换它(假设您使用的是数据库),或者执行以下操作(假设您的格式错误latin1 / { {1}}导致数据错误的原因:

ISO-8859-1

您无法强制foreach ($row as $key => &$value) { if ( ! mb_check_encoding($value, "UTF-8")) { // assume base is latin1; your mileage may vary $value = mb_convert_encoding($value, "UTF-8", "ISO-8859-1"); } } 解析无效数据,因此您必须将数据集按到有效的UTF-8中。