可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Take this code:
$json = file_get_contents($this->url, true); $decode = json_decode($json, true); foreach ($decode as $key => $value) { ... }
Pretty simple, uh?
Passing a $json with up to 500 array elements.... works right!
Above that limit... the error is:
Warning: Invalid argument supplied for foreach() in /c/website/retriever/WsGlassRetriever.php on line 19
Is there some memory limit for that function's argument?
I didn't found nothing about it in the docs. My version is PHP 5.2.17-rnx1.1 with Suhosin-Patch 0.9.7 (cli)
回答1:
json_decode
returns NULL
if there is an error in the JSON syntax. I've just successfully tested on an array of 1000 elements and it ran just fine.
Double-check that your JSON is correctly formatted. Even something as small as having single quotes instead of double, or forgetting to put a property name in quotes, or using a character outside the 32-127 range without correctly encoding it in UTF-8 can cause these problems.
回答2:
Am sure your JSON code above 500 has a formatting issue , have used JSON with over 20,000 values here is a simple script of 2000 array
Edit 2
I was so interested to know if there is any limitation .. just tested it with 250,000 (Two hundred and fifty thousand values and it works fine )
Thanks Oleku
回答3:
In my case the JSON was correct. My problem was the "JSON_BIGINT_AS_STRING" parameter that was causing the error "Maximum stack depth exceeded".
$jsonResult = json_decode($expr,true,JSON_BIGINT_AS_STRING);
I removed the "JSON_BIGINT_AS_STRING" argument and the error is gone:
$jsonResult = json_decode($expr,true);