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

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.

deceze
  • 510,633
  • 85
  • 743
  • 889
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753

5 Answers5

1

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

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
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;
}
Giel Berkers
  • 2,852
  • 3
  • 39
  • 58
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;
}
CHATHURA
  • 106
  • 7
  • This will not work. The array is too big for the json_encode. You are trying to spit it when it already crashed – Wow Oct 26 '17 at 14:45
0

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

memory_limit=256M
Hix.botay
  • 387
  • 3
  • 10
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

mdehghani
  • 504
  • 1
  • 7
  • 23