BlobbyBob BlobbyBob - 13 days ago 5
JSON Question

PHP - json_encode and array size

[PHP version 5.5.9]

I have an array similar to the following:


array(2) {
["items"]=>
array(2) {
[0]=>
array(3) {
["title"]=>
string(5) "Item1"
["price"]=>
int(999)
["img"]=>
string(38) "http://someu.rl"
}
[1]=>
array(3) {
["title"]=>
string(5) "Item2"
["price"]=>
int(1999)
["img"]=>
string(38) "http://someu.rl"
}
}
["success"]=>
bool(true)
}


When I use
json_encode()
on this array I have no problems until the array contains 4 (or more) items. Then it returns false instead of the JSON string.

I once exported the array (with 12 items) using
var_export()
and inserted it manually into
json_encode()
(originally the data is from a database) and then it worked.


Has anyone an idea what the problem is? Thanks for answers in advance.

Answer

http://php.net/manual/en/function.json-encode.php

Returns a JSON encoded string on success or FALSE on failure.

There is a 99.99% chance, that your "longer" string contains unescaped characters or other invalid stuff.

There is no "limit" in the size of an json array, neither does json-encode have. (Beside natural system limitations, which you wont hit with 4 elements)