-1
$lts = mysqli_fetch_all($con->query($query), MYSQLI_NUM);
for($i=0; $i<count($lts); $i++){
    for($j=0; $j<count($lts[$i]); $j++){
        $lts[$i]=$lts[$i][$j];
    }
}

Here is $lts var_dump:

array (size=2)
  0 => string '20' (length=2)
  1 => string '21' (length=2)

Is giving me this Warning

Parameter must be an array or an object that implements Countable

This just happened when upgrading from PHP7.1 to 7.2. Any idea how to fix it?

Kevin S
  • 173
  • 2
  • 10
  • 2
    Make sure what you are putting into it, is actually an array ;) – IncredibleHat Dec 21 '17 at 15:53
  • 2
    Check that the return of `mysqli_fetch_all()` contains something - could be null for no rows. – Nigel Ren Dec 21 '17 at 15:54
  • 5
    .. and check release notes... they added this warning in 7.2 http://php.net/manual/en/migration72.incompatible.php "An E_WARNING will now be emitted when attempting to count() non-countable types (this includes the sizeof() alias function)." – DDeMartini Dec 21 '17 at 15:54
  • We have to assume the query failed or returned no rows – RiggsFolly Dec 21 '17 at 15:57
  • @RiggsFolly If it failed, `mysqli_fetch_all()` would complain of a boolean argument. If it returned no rows, `$lts` should be an empty array. – Barmar Dec 21 '17 at 16:03
  • Going by your edit, you've got an array of strings, not the two-dimensional array that nested for loops would require. So it's the second loop that's returning the error. – iainn Dec 21 '17 at 16:13
  • `count($lts[$i])` is the place that raises the warning… – deceze Dec 21 '17 at 16:14
  • @iainn `mysqli_fetch_all()` returns a 2-D array. That must be a dump from after the loop is done. – Barmar Dec 21 '17 at 16:17
  • Yeah, it’s the second count() that throws the error. Any idea how to fix it? – Kevin S Dec 21 '17 at 16:30

3 Answers3

1

The problem is that the first time through the inner loop, you're replacing $lts[$i] with $lts[$i][$j]. When the loop repeats, it tests $j < count($lts[$i]). But $lts[$i] is no longer an array, it's the value from the first column of the row, so you get this error.

You can solve this by assigning count($lts[$i]) to a variable before the loop. But that just raises another problem. When you try to do $lts[$i] = $lts[$i][$j] in subsequent iterations, $lts[$i] is still not an array, so there's no $j element of it.

You can solve that by using foreach instead of for, since it makes a copy of the array it's looping over.

for($i=0; $i<count($lts); $i++){
    foreach ($lts[$i] as $col)
        $lts[$i] = $col;
    }
}

But it's not clear what the point of the inner loop is. Each iteration just overwrites $lts[$i] with the next column, so the final result will just be the last column. You can do that without the inner loop.

foreach ($lts as &$row) {
    $row = end($row);
}

or simply:

$lts = array_map('end', $lts);
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Wordpress 4.9.8 theme installs (php 7.2 count() Warning) :

Warning: count(): Parameter must be an array or an object that implements Countable in /var/www/html/wp-content/themes/office/admin/functions/functions.mediauploader.php on line 127

Solution;

line 127 changed...

if ( count( $_posts ) ) {

to

if ( is_array( $_posts ) ) {

Community
  • 1
  • 1
-1

Create a new function to replace count

function count2($x) {
    return is_array($x) ? count($x) : 0;
}
jai3232
  • 383
  • 3
  • 6