afazolo
afazolo

Reputation: 382

count(): Parameter must be an array or an object that im plements Countable error based on the array size

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?

Upvotes: 0

Views: 614

Answers (2)

afazolo
afazolo

Reputation: 382

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!

Upvotes: 0

Renziito
Renziito

Reputation: 78

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

Upvotes: 2

Related Questions