I have a php array structure
我有一个php数组结构
Array
(
[car] => Array
(
[red] => 0.333
[orange] => 0.333
[blue] => 0.333
)
[truck] => Array
(
[white] => 0.333
[green] => 0.333
[blue] => 0.333
)
)
I have been using serialize to save the array to a text file, and unserialize to get back the array form. Unfortunately, the serialized array is getting very large, but mostly due to the float point (bug or by design) conversion when serialized. For example, instead of 0.333, the serialized process converts .333 into .3333333333333333333333333333333333333333333333333. This made me want to switch to json_encode to save the array. When compare serialize to json_encode, the serialized file is 40MB in size vs 8MB size for json_encode.
我一直在使用序列化将数组保存到文本文件,并反序列化以返回数组表单。不幸的是,序列化数组变得非常大,但主要是由于序列化时的浮点(错误或设计)转换。例如,序列化过程将.333转换为.3333333333333333333333333333333333333333333333333,而不是0.333。这让我想切换到json_encode来保存数组。比较serialize和json_encode时,序列化文件的大小为40MB,而json_encode的大小为8MB。
That is great, except when I try to json_decode the file, it is no longer in the array form. I tried json_decode($array, true), but that does not work either.
这很好,除非我尝试json_decode文件,它不再是数组形式。我尝试过json_decode($ array,true),但这也不起作用。
Any idea how to get json_encode to work for this example?
知道如何让json_encode适用于这个例子吗?
TIA
PS, my floating point number was generated by rounding off the decimals. Another answer that I found on StackO suggested that instead of using round($part/$sum, 3);
, use sprintf('%.3f', $part/$sum);
which turned the floating point into a string. That alone cut the serialized file down from 40MB to 19MB, but it still much larger than the json_encode file size of 8MB.
PS,我的浮点数是通过舍入小数生成的。我在StackO上找到的另一个答案建议不要使用round($ part / $ sum,3);而是使用sprintf('%。3f',$ part / $ sum);它将浮点转换为字符串。仅此一项就将序列化文件从40MB减少到19MB,但它仍然比json_encode文件大小8MB大得多。
1 个解决方案
#1
0
The 'problem' is due to json_decode inability to read large json_encode files. The largest json file that can work is only ~.5MB. Tested on 4GB Ram, 4 core Xeon server, and also 4gb localhost laptop. I also had set memory_limit in the php.ini file to be 3GB for other php routines (yes, 3GB) and restarted apache. So the memory_limit setting appears not to be the problem.
'问题'是由于json_decode无法读取大型json_encode文件。可以工作的最大json文件只有〜。5MB。测试了4GB Ram,4核Xeon服务器以及4GB localhost笔记本电脑。我还将php.ini文件中的memory_limit设置为3GB用于其他php例程(是的,3GB)并重新启动apache。所以memory_limit设置似乎不是问题。
Error message was not helpful, it stated that
错误消息没有帮助,它说明了这一点
Warning: array_slice() expects parameter 1 to be array, null given in /home/xxxxx/public_html/xxxx.php on line xx
警告:array_slice()期望参数1为数组,在xx行的/home/xxxxx/public_html/xxxx.php中给出null
Hopefully this error message would help some person in the future to narrow down the bug.
希望这个错误信息可以帮助将来的某些人缩小bug。
#1
0
The 'problem' is due to json_decode inability to read large json_encode files. The largest json file that can work is only ~.5MB. Tested on 4GB Ram, 4 core Xeon server, and also 4gb localhost laptop. I also had set memory_limit in the php.ini file to be 3GB for other php routines (yes, 3GB) and restarted apache. So the memory_limit setting appears not to be the problem.
'问题'是由于json_decode无法读取大型json_encode文件。可以工作的最大json文件只有〜。5MB。测试了4GB Ram,4核Xeon服务器以及4GB localhost笔记本电脑。我还将php.ini文件中的memory_limit设置为3GB用于其他php例程(是的,3GB)并重新启动apache。所以memory_limit设置似乎不是问题。
Error message was not helpful, it stated that
错误消息没有帮助,它说明了这一点
Warning: array_slice() expects parameter 1 to be array, null given in /home/xxxxx/public_html/xxxx.php on line xx
警告:array_slice()期望参数1为数组,在xx行的/home/xxxxx/public_html/xxxx.php中给出null
Hopefully this error message would help some person in the future to narrow down the bug.
希望这个错误信息可以帮助将来的某些人缩小bug。