0

The first line is the problem code. I don't know how to change that count to something that can work.

if(count($item[2]) > 0){
    if($item[2][0] == 'plane' || $item[2][0] == 'url'){
        if($item[2][0] == 'url'){
            $arr = explode('file/d/',$id);
            $arr1 = explode('/',$arr[1]);
            $id = $arr1[0];
        }
   }
 }
?>
Kelvin Mariano
  • 991
  • 8
  • 15
AssortDis
  • 3
  • 4
  • Hi, this is probably caused by the fact that the **$item** variable is not of countable type and thus the **count()** function cannot be used. What is inside **$item[2]** and what does **$item** hold in the first place? – Eugenio Dec 16 '19 at 08:50

3 Answers3

2

In PHP 7.2, a Warning was added while trying to count uncountable things. To make it fix change this line:

if(count($item[2]) > 0){

with this:

if(is_array($item[2]) && count($item[2]) > 0){

In PHP 7.3 a new function was added is_countable, specifically to address the E_WARNING issue. If you are using PHP 7.3 so you can change this line:

if(count($item[2]) > 0){

with this:

if(is_countable($item[2]) && count($item[2]) > 0){
Sami Ahmed Siddiqui
  • 2,328
  • 1
  • 16
  • 29
0

I believe that in some cases this $item[2] returns null or any other non-countable value. Starting from PHP 7, you will not be able to count an object that not implementing countable. So you need to check first if it's an array:

if(is_countable($item[2])){ // you can also use is_array($item[2])
    if(count($item[2]) > 0){
        //rest of your code
    }
}

Another way (though not preferred) is to pass your object to ArrayIterator. That will make it iteratable:

$item_2 = new ArrayIterator($item[2]);
if(count($item_2) > 0){
   //rest of your code
}
MEDZ
  • 2,227
  • 2
  • 14
  • 18
0

Try code below:

if (is_array($item[2]) || $item[2] instanceof Countable || is_object($item[2])) {
    if(count($item[2]) > 0){
        if($item[2][0] == 'plane' || $item[2][0] == 'url'){
            if($item[2][0] == 'url'){
                $arr = explode('file/d/',$id);
                $arr1 = explode('/',$arr[1]);
                $id = $arr1[0];
            }
        }
    }
}

Check it

Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42