7

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)

7
  • 1
    What does var_dump($decode) give you? Commented Mar 28, 2012 at 13:24
  • 1
    i dont think its limit. Its not limit. try var_dump($decode) to see type Commented Mar 28, 2012 at 13:24
  • 1
    it may be not because of the count of the elements, but because of particular element that can't be decoded. In php 5.3 there is json_last_error() function. Also mind the depth limit (512 by default) Commented Mar 28, 2012 at 13:24
  • 1
    You can use json_last_error() [edit: requires PHP >= 5.3!] to determine what exactly went wrong: php.net/manual/en/function.json-last-error.php Commented Mar 28, 2012 at 13:25
  • 1
    @Darhazer 512 now, but used to be 128 and before that used to be 20. If your pre-5.2.3 this can be a real problem, but the OP is 5.2.17 so his is 128. Commented Mar 28, 2012 at 13:29

3 Answers 3

15

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok. it was a malformed utf-8 error. I looked at my json and there I found a 'ì' char which was bad utf8... What about validating my utf-8 json on the fly with php?
I use GuzzleHttp's json_decode wrapper, it throws an InvalidArgumentException when JSON data is bad
9

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

$string = "Sample String Data ¶";
$string = preg_replace( '/[^[:print:]]/', '',$string); // remove all values that can affect JSON 
$array = array();
for($i = 0 ; $i <  2000; $i++)
{
    if(mt_rand(0, 1))
    {
        $array[] = $string ;
    }
    else
    {
        $array[] = array($string,1,$string) ;
    }
}   

$json =  json_encode($array);
$decodeArray =  json_decode($json);

switch (json_last_error()) {
    case JSON_ERROR_NONE:
        echo ' - No errors';
        break;
    case JSON_ERROR_DEPTH:
        echo ' - Maximum stack depth exceeded';
        break;
    case JSON_ERROR_STATE_MISMATCH:
        echo ' - Underflow or the modes mismatch';
        break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' - Unexpected control character found';
        break;
    case JSON_ERROR_SYNTAX:
        echo ' - Syntax error, malformed JSON';
        break;
    case JSON_ERROR_UTF8:
        echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
        break;
    default:
        echo ' - Unknown error';
        break;
}

echo "<br />" ;


foreach ($decodeArray as $key => $value) {
    print_r($value) ;
    flush();
}

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

Comments

0

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);

1 Comment

Your problem might have been the fact that you defined max depth with JSON_BIGINT_AS_STRING, which equals 2. You should have done $jsonResult = json_decode($expr, true, 512, JSON_BIGINT_AS_STRING);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.