0

i have an array $captions and i want to check if there is at least one value inside or not. the array could looks like that:

Array
(
    [0] => 84
    [1] => 
    [2] => 82
    [3] => 
    [4] => 
    [5] => 
    [6] => 
    [7] => 
    [8] => 
    [9] => 
)

and my way to check if there is "more than 0", "more than 1"...

if (count($captions) > 0)   { echo '<br>bigger than 0';}
if (count($captions) === 1) {echo '<br>is 1';};
if (count($captions) > 1)   {echo '<br>gbigger than 1';};
if (count($captions) > 2)   {echo '<br>bigger than 2';}

but: it gives me with this array above follwing result:

bigger than 0
bigger than 1
bigger than 2

"bigger than 2" should not be, because the array contains only two values? what am i getting wrong?

TarangP
  • 2,711
  • 5
  • 20
  • 41
Cycle99
  • 172
  • 1
  • 3
  • 16

3 Answers3

6

You need to do like this (using array_filter()):-

echo  "equal to" . count(array_filter($array));

Output:- https://eval.in/897055 Or https://eval.in/897063

Note:- In your code you are counting empty values too, hense ambiguity occur. You need to first remove empty values and then need to count.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
-1

Use the php array_filter function

array_filter($array)
Arafath
  • 1,090
  • 3
  • 14
  • 28
-1

Perform

echo  count(array_filter($captions));
TarangP
  • 2,711
  • 5
  • 20
  • 41