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?
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?
To count a value in a two dimensional array, here is the useful snippet to process and get count of a particular value-
<?php
$list = [
['id' => 1, 'userId' => 5],
['id' => 2, 'userId' => 5],
['id' => 3, 'userId' => 6],
];
$userId = 5;
echo array_count_values(array_column($list, 'userId'))[$userId]; // outputs: 2