PHP - count specific array values

Ivo San picture Ivo San · Jul 25, 2012 · Viewed 125.3k times · Source

How can I count the number of element inside an array with value equals a constant? example,

$myArray = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");

how can I directly know how many "Ben" is inside?

Answer

Rajat Singhal picture Rajat Singhal · Jul 25, 2012
$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");
$counts = array_count_values($array);
echo $counts['Ben'];