PHP json_encode大数组 [英] php json_encode big array

查看:367
本文介绍了PHP json_encode大数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个大数组上使用json_encode,结果不返回任何内容(是的,我检查了它是utf-8).当我开始调查此问题时,我发现当字符串大于65536时会出现此问题.

因此,当我的数组大小为1245时,其来自json_encode的字符串的长度为string(65493),但是当我将数组增加一时,字符串变得比65536长,json_encode无法输出任何结果. /p>

我认为问题是由于memory limit引起的,但是当我检查php.ini时,我发现它是-1.

有什么问题吗?

基本上我正在做这样的事情:

$arr = array();
for($i =0; $i<9000; $i++){
    $arr[] = array(
        'name'  => 'test',
        'str'   => md5($i)
    );
}
echo '<pre>'.json_encode($arr).'</pre>';

PS .对不起.我发现了问题,这要感谢一个名字不可重印的人:-)(谢谢劳伦斯). <pre>是罪魁祸首...由于某种原因,它不会在我的浏览器中显示字符串,但是它在那里.

劳伦斯,如果您愿意的话,您可以只写它,我会接受它的正确性.因为您是我提出这个建议的原因.

解决方案

只是为了消除对此问题的困惑.答案已经找到并且在问题中.

json_encode函数没有任何问题.它对每个输出均正确工作.除了您的内存以及您为脚本分配的内存量之外,没有其他限制.

问题出在浏览器的<pre>标签实现上.如果为该标签提供太大的字符串,它将不会打印任何内容.因此,解决方法是在没有<pre>标签的情况下输出答案

I am trying to use json_encode on a big array, and the result returns nothing (yes, I checked that it is utf-8). When I started to investigate this issue I found that the problem arise when a string becomes bigger than 65536.

So when my array is of size 1245, its string from json_encode has length of string(65493), but when I increase array by just one, the string becomes longer than 65536, json_encode fails to output any result.

I thought that the problem is because of memory limit, but when I checked my php.ini I see that it is -1.

Any idea what can be a problem?

Basically I am doing something like this:

$arr = array();
for($i =0; $i<9000; $i++){
    $arr[] = array(
        'name'  => 'test',
        'str'   => md5($i)
    );
}
echo '<pre>'.json_encode($arr).'</pre>';

P.S. sorry guys. I found the problem, thanks to a person with an unreprintable name :-) (thank your Lawrence). <pre> is the culprit... for some reason it does not print the string in my browser, but it is there.

Lawrence, if you want, you can just write it and I will accept it as correct. Because you were the reason that I came up with this.

解决方案

Just to remove confusion about this question. The answer is already found and it is in the question.

There is nothing wrong with json_encode function. It works correctly for every output. There is no limitation there except of your memory and how much of it are you giving to your script.

The problem was with browser's implementation of <pre> tag. If you provide too big string to this tag it does not print anything. So the way out is to output answer without <pre> tag

这篇关于PHP json_encode大数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆