To reduce the total cycles needed to isolate the elements that occur more than once, I'll demonstrate a foreach loop that will initialize a lookup array with a -1 value then increment it for every occurrence.
array_filter()
is called to remove entries in the lookup which only got up to 0. This will leave only values that have a positive count after excluding the first occurrence.
Code: (Demo) (Alternative Syntax)
$array = [
['KRA_category'=>'Business'],
['KRA_category'=>'Business'],
['KRA_category'=>'People'],
['KRA_category'=>'Business'],
['KRA_category'=>'Business'],
['KRA_category'=>'Business']
];
foreach ($array as ['KRA_category' => $value]) {
$result[$value] ??= -1;
++$result[$value];
}
var_export(array_filter($result));
Output:
array (
'Business' => 4,
)
You can achieve the same result with a functional-style approach but this will require far greater computational time complexity. (Demo)
var_export(
array_count_values(
array_diff_key(
array_column($array, 'KRA_category'),
array_unique($array, SORT_REGULAR)
)
)
);