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
49.3k13 gold badges98 silver badges165 bronze badges
asked May 11, 2017 at 9:13
5
  • 1
    array_count_values() - php.net/manual/en/function.array-count-values.php Commented May 11, 2017 at 9:13
  • 2
    Possible duplicate of PHP - count specific array values Commented May 11, 2017 at 9:19
  • 1
    @KrisRoofe array_count_values can't be used for multi-dimensional arrays. Commented May 11, 2017 at 10:21
  • @Yeah, need some work before array_count_values Commented May 11, 2017 at 10:29
  • Why is it business = 4? There are 5 elements with KRA_category => Business. Commented Jul 16, 2022 at 12:23

3 Answers 3

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.

answered May 11, 2017 at 10:24
Sign up to request clarification or add additional context in comments.

9 Comments

This does not meet the brief: i need count of duplicate values in array for e.g. business=4.
@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.
See how my answer ignores the first encountered value and only counts subsequently encountered values. I don't see any fogginess in the question.
Simply subtracting 1 from array_count_values seems simpler to me.
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.
|
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.

LF-DevJourney
28.6k30 gold badges170 silver badges322 bronze badges
answered May 11, 2017 at 10:25

1 Comment

This does not meet the brief: i need count of duplicate values in array for e.g. business=4.
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)
 )
 )
);
answered May 11, 2017 at 14:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.