0

This error is generated in the code below only when the returned array is too long. With short arrays (I do not know exactly how much) does not occur.

$phone_numbers = array();
if(!empty($_POST['phone_numbers']))
    $phone_numbers = json_decode($_POST['phone_numbers']);
    $phone_numbers_var = str_repeat('?,', count(json_decode($_POST['phone_numbers'])) - 1) . '?'; // <-- error line

Is there a limit to the count () parameter?

mdoflaz
  • 551
  • 6
  • 19
afazolo
  • 382
  • 2
  • 6
  • 22
  • Is this a PHP question? – Machavity Jun 26 '19 at 20:05
  • 1
    Why don't you use `count` on the `$phone_numbers` instead of `json_decode` twice? And no, there is no limit on `count` – dWinder Jun 26 '19 at 20:39
  • @dWinder I've tried both ways and the error persists – afazolo Jun 26 '19 at 21:12
  • Unless it was double encoded, the second decode will return null regardless of the array size. – Don't Panic Jun 26 '19 at 21:18
  • Possible duplicate of [count(): Parameter must be an array or an object that implements Countable](https://stackoverflow.com/questions/48343557/count-parameter-must-be-an-array-or-an-object-that-implements-countable) – miken32 Jun 26 '19 at 22:13

2 Answers2

2

First check you $_POST['phone_numbers'] what is getting

remember that :

var_dump(count(null));var_dump(count(false));

will output :

Warning: count(): Parameter must be an array or an object that implements Countable in

I think is the PHP version 7.2's count is a little weird... but you can try something like this :

https://wiki.php.net/rfc/counting_non_countables

EDIT :

For just comment :

$POST['phone_numbers'] = [165567, 545675, 655666];

if you try to do this:

json_decode($POST['phone_numbers']);

will return this :

WARNING json_decode() expects parameter 1 to be string, array given on line number 4

and count of that... you know .. just do:

count($POST['phone_numbers']);
Renziito
  • 78
  • 3
0

Is funny but it seems that if in the array there is some digit starting with 0 the error occurs. When I remove the initial 0 is ok.

 $phone_numbers = [011364346387, 33334444, ..., n] //error
 $phone_numbers = [11364346387, 33334444, ..., n] //is ok!
afazolo
  • 382
  • 2
  • 6
  • 22