0

I need count of duplicate values in my array.

[ 
    ['KRA_category' => 'Business'],
    ['KRA_category' => 'Business'],
    ['KRA_category' => 'People'],
    ['KRA_category' => 'Business'],
    ['KRA_category' => 'Business'],
    ['KRA_category' => 'Business'],
];

Expected result:

['Business' => 4] 
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Shubhangi
  • 69
  • 3

3 Answers3

1

You can combine array_column and array_count_values

$counts = array_count_values(array_column($array, 'KRA_category'));

For Business you would then use

$business_count = $counts['Business'] - 1;

to get the duplicates. -1 is to ignore the original and only count the extras.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This does not meet the brief: `i need count of duplicate values in array for e.g. business=4`. – mickmackusa Jul 16 '22 at 11:23
  • @mickmackusa This gets the counts for every value. I've updated the answer to show how to get a the count for a specific value from that. I'm not sure how the OP is getting 4 instead of 5, I've asked them. – Barmar Jul 16 '22 at 12:24
  • See how my answer ignores the first encountered value and only counts subsequently encountered values. I don't see any fogginess in the question. – mickmackusa Jul 16 '22 at 12:38
  • Simply subtracting 1 from array_count_values seems simpler to me. – Barmar Jul 16 '22 at 12:39
  • Then you have to filter out the zeros. This takes several cycles as I mention in my answer. My old answer (from years back) called `array_walk()` to decrement, but it felt too messy to me. – mickmackusa Jul 16 '22 at 12:44
  • You don't have to filter out anything, just subtract 1 from the value you're interested in. – Barmar Jul 16 '22 at 12:45
  • How do you know which values you are interested in before counting all values. The reason that the sample data's output only has one entry is because `People` only occurs once. If `People` occurred twice, it would also be in the result array with a value of 1 -- meaning one original and one duplicate. I hope the asker returns from their 3 year hibernation. – mickmackusa Jul 16 '22 at 12:48
  • You don't need to. When you're looking up what you want, subtract 1. If you need to prevent ever seeing the zeroes, you can do one array filter after you're done counting, just like in your answer. – Barmar Jul 16 '22 at 12:51
  • I am not inventing requirements here. The asker's title is `count of duplicate values in array`. Not `count duplicates for a specific key in the array`. It is situational that the [mcve] only has one value in the results. I can't argue that the mcve is high quality; I'd certainly prefer a better sample input. – mickmackusa Jul 16 '22 at 12:54
0

you can count the duplicate values in php with

$vals = array_count_values(array_column($array, 'KRA_category'));

supported in 5.5 and above.

LF00
  • 27,015
  • 29
  • 156
  • 295
0

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)
        )
    )
);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136