$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");
$counts = array_count_values($array);
echo $counts['Ben'];
Answer from Rajat Singhal on Stack Overflow$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");
$counts = array_count_values($array);
echo $counts['Ben'];
You can do this with array_keys and count.
$array = array("blue", "red", "green", "blue", "blue");
echo count(array_keys($array, "blue"));
Output:
3
function array_count_values_of($value, $array) {
$counts = array_count_values($array);
return $counts[$value];
}
Not native, but come on, it's simple enough. ;-)
Alternatively:
echo count(array_filter($array, function ($n) { return $n == 6; }));
Or:
echo array_reduce($array, function (
n) { return
n == 6); }, 0);
Or:
echo count(array_keys($array, 6));
This solution may be near to your requirement
$array = array(1, 2, 3, 3, 3, 4, 4, 5, 6, 6, 6, 6, 7);
print_r(array_count_values($array));
Result:
Array
( [1] => 1 ,[2] => 1 , [3] => 3, [4] => 2,[5] =>1, [6] => 4, [7] => 1 )
for details.
As I can't comment, the philonus's answer is the clearer and best for me, if you want to display zero you can do this, initializing the stats array:
$input = array(
"report=D","report=D","report=D","report=I","report=I"
);
$stats = array(
'U' => 0,
'D' => 0,
'I' => 0,
);
foreach($input as $value) {
$types = explode('=', $value);
$stats[$types[1]]++;
}
print_r($stats);
I am not really an expert in PHP, but this can certainly be done a bit easier. First of all it seems as if you would need to iterate over the input array only once (while the ordering of the array does not matter). Hence you can use a foreach($array_to_work_on, $single_value) construct and do all the logic therein.
So the first thing to do is to split each value. You can use explode which takes as a first argument the string where the input value should be split and as a second the input itself. Since we are not interested in the first part we take only the second one (with the index 1).
Then you can make use of the "array" (or rather dictionary) data structure provided by PHP: Whenever we encounter an unseen "key" we add a new entry to the dictionary and save "1" as the number of already counted elements. In the other case (if we have already seen this key) we increment it by one.
So here is the code:
$input = array(
"report=D","report=D","report=D","report=I","report=I"
);
$stats = array();
foreach($input as $value) {
$type = explode('=', $value)[1];
$stats[$type] = array_key_exists($type,$stats) ? $stats[$type] + 1 : 1;
}
print_r($stats);
Note that now you also sum up also the frequency of other keys, not only "I", "O", and "U", which makes it easier to maintain or to extend.
Edit: As has been pointed out in the comments, one cannot use "function array dereferencing", i.e. $type = explode('=', $value)[1]; in older PHP versions. So the solution in this case would be to introduce a temporary variable:
$parts = explode('=', $value);
$type = $parts[1];
Use array_count _values to get an array with everything counted for you.
Here is just an idea. You could use array_keys($myArray, "") using the optional second parameter which specifies a search-value. Then count the result.
$myArray = array("", "", "other", "", "other");
$length = count(array_keys($myArray, ""));
You can use count with array_filter
function doSearch($id, array $array) {
$arr = array_filter($array, function ($var) use ($id){
if ($id === $var['parent_id']) {
return true;
}
});
return count($arr);
}
or shorter version
function doSearch($id, array $array) {
return count(array_filter($array, function($var) use ($id) {
return $id === $var['parent_id'];
}));
}
So basically it gets elements where $id is equal to parent_id and returns their count
You could implement it using array_filter, to filter the array down to the elements that match a given parent id, and then return the count of the filtered array. The filter is provided a callback, which is run over each element of the given array.
function find_children($array, $parent) {
return count(array_filter($array, function ($e) use ($parent) {
return $e['parent_id'] === $parent;
}));
}
find_children($array, 1); // 2
find_children($array, 3); // 1
find_children($array, 10); // 0
Whether or not you prefer this to a loop is a matter of taste.
You can approach this two ways, fixing the current idea would mean having to extract the status column from the results element of the data and then using array_keys(), as currently the arrays it's looking at are at the wrong level...
echo count(array_keys(array_column($result['results'], 'status'), "open"));
Which is short, but also involves processing the whole array a few times.
Alternatively a simple foreach() loop just counting each time it's the value your after...
$count = 0;
foreach ( $result['results'] as $res ) {
if ( $res['status'] == 'open' ) {
$count++;
}
}
echo $count;
This (IMHO) is faster and more readable.
You can use count with array_filter like below,
echo count(array_filter($arr['results'], function ($val) {return $val['status'] == 'open';}));
Demo
Output
2
Note: Your json is invalid.