7

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>';
6
  • Please make sure that error reporting is enabled Commented Aug 26, 2013 at 23:44
  • 1
    Can you re-reproduce the behaviour, in an example. Commented Aug 26, 2013 at 23:52
  • It does not print the string but it is actually there? Now that's even weirder. :P Commented Aug 27, 2013 at 0:02
  • please take a look at this stackoverflow.com/a/6194563/562036 according to the answer provided there doesn't seem to be a limit to json_encode(), however this number 65536 seemed to the the string limit in java how did you get that? Commented Aug 27, 2013 at 1:49
  • Do you try php.net/manual/pt_BR/function.json-last-error.php ? Commented Aug 27, 2013 at 3:52

5 Answers 5

1

I had the same problem and the array was so big that increasing the memory limit didn't solve my problem. Had to write my own jsonEncode()-method to overcome this:

/**
 * Alternative to json_encode() to handle big arrays
 * Regular json_encode would return NULL due to memory issues.
 * @param $arr
 * @return string
 */
private function jsonEncode($arr) {
    $str = '{';
    $count = count($arr);
    $current = 0;

    foreach ($arr as $key => $value) {
        $str .= sprintf('"%s":', $this->sanitizeForJSON($key));

        if (is_array($value)) {
            $str .= '[';
            foreach ($value as &$val) {
                $val = $this->sanitizeForJSON($val);
            }
            $str .= '"' . implode('","', $value) . '"';
            $str .= ']';
        } else {
            $str .= sprintf('"%s"', $this->sanitizeForJSON($value));
        }

        $current ++;
        if ($current < $count) {
            $str .= ',';
        }
    }

    $str.= '}';

    return $str;
}

/**
 * @param string $str
 * @return string
 */
private function sanitizeForJSON($str)
{
    // Strip all slashes:
    $str = stripslashes($str);

    // Only escape backslashes:
    $str = str_replace('"', '\"', $str);

    return $str;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

Please try this,

$arr = array();
for($i =0; $i<3000; $i++){
$arr[] = array(
    'name'  => 'test',
    'str'   => md5($i)
);
}
$contentArr = str_split(json_encode($arr), 65536);
foreach ($contentArr as $part) {
    echo $part;
}

1 Comment

This will not work. The array is too big for the json_encode. You are trying to spit it when it already crashed
0

It also occur if the array exceed memory limit, you can try change memory_limit in php.ini like

memory_limit=256M

Comments

0

in my case I found out that the array (derived from my database) contains strings including special characters so I made sure to convert them to utf-8 before using json_encode() function. more on that: explained here

Comments

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.