Hi

Is it possible to count the values within an array that match specific criteria.

Lets say you have an array:

$myarray = array("dogs","cats","mice","dogs","cows","dogs","mice");

if i do count($myarray) i will obviously get 7.

What about if i want to know how mnay times "dogs" appears in the array. I've been playing with in_array but that only returns a true (1) or false (0) value:

echo in_array("dogs", $myarray);
//output is 1

I want to count how many times the word "dogs" is in that array.

Any ideas? I'm sure this is possible?

Cheers Ant

    I'm not sure, once compiled in C, which way is less processing, but I'm pretty sure he did it the smart way. You could, also, do:

    $myarray = array("dogs","cats","mice","dogs","cows","dogs","mice");
     foreach ($myarray as $key) {
       if ($key == "dogs") $i++;
     }
     echo $i;
    
      Write a Reply...