0

How can I set the maximum depth limit to unlimited for json_encode function in PHP?

My problem is;

$array = 
[
    [
       [
          ['hello'],
       ],
    ],
];

Json_encode function returns false when I add more than 3 arrays multidimensionally. I wanna remove this limit. How can I do it?

Thanks.

1 Answers1

-2

The json_encode function accepts a depth parameter: https://www.php.net/manual/en/function.json-encode

<?php
$array = 
[
    [
       [
          ['hello'],
       ],
    ],
];

$text = json_encode($array, 8);
?>
pyb
  • 4,813
  • 2
  • 27
  • 45
  • Since the default is 512, this won't fix the problem. And the error message is not about the depth. – Barmar Jan 14 '21 at 21:16
  • @Barmar This answers the question being asked. I see the discussion in the comments, the question should be updated accordingly. – pyb Jan 14 '21 at 21:19
  • It would only answer the question being asked if his arrays were nested more than 512 levels. – Barmar Jan 14 '21 at 21:20
  • That is a good idea. That was not obvious to me however. OP said "I can run this script my localhost. But I can't use on hosting." so it could have been a limitation on the host behalf. – pyb Jan 14 '21 at 21:38