1

I have this array:

Array ( [55118] => Array ( [id] => 55118 
                           [usr_name] => Name 1
                           [usr_employment] => Fulltime
                         )
        [55179] => Array ( [id] => 55179
                           [usr_name] => Name 2
                           [usr_employment] => Fulltime
                         )
        [55549] => Array ( [id] => 55549
                           [usr_name] => Name 1
                           [usr_employment] => Fulltime
                         )
      )

Now 'd like to count how many times "Name 1" exist in my array.
My problem is that it's a 2D array.

So i'd like to print:

Name 1, 2 times
Name 2, 1 time

I can't find a correct answer to this question before.

Björn C
  • 3,860
  • 10
  • 46
  • 85
  • 2
    Does this help? https://stackoverflow.com/q/4948946 – Praveen Kumar Purushothaman May 29 '18 at 08:37
  • Please tell me why downvote so i can prevent this in the future! – Björn C May 29 '18 at 08:38
  • 2
    Not the downvoter but possibly downvoted because this is a commonly asked question – IsThisJavascript May 29 '18 at 08:39
  • 1
    @BjörnC I didn't down-vote but it would be considered a duplicate. Just do a bit more searching first before posting. – Pogrindis May 29 '18 at 08:39
  • 1
    Because most likely the person that downvoted thinks that you didn't provide the solution you have tried as a minimal code, or specific errors – Gerasimos Ragavanis May 29 '18 at 08:40
  • Don"t worry about downvote, this may help you : https://stackoverflow.com/questions/25221390/php-count-duplicate-values-within-two-dimensional-array-then-display-only-uni – AIT MANSOUR Mohamed May 29 '18 at 08:41
  • @PraveenKumar No. I only wan't to count the duplicates. Not remove them! – Björn C May 29 '18 at 08:42
  • @Pogrindis You guys are so fast marking it as a duplicate. Your Link will remove all duplicates. I'd just wana count them! – Björn C May 29 '18 at 08:43
  • @BjörnC so you can do compare of the two when you have removed from another instance, there are several solutions to the problem, the speed of the duplicate is on the right hand sidebar, there are **related** questions. These usually show duplicates. – Pogrindis May 29 '18 at 08:45
  • Well, a foreach can make what you do no? Or you want to use a php array function? – Mickaël Leger May 29 '18 at 08:45
  • Try this: foreach($yourArray as $key => $value){ $newArr[] = $value['usr_name']; } $counts = array_count_values($newArr); foreach($counts as $name => $count){ echo $name." - ".$count." times
    " }
    – prit.patel May 29 '18 at 08:47
  • 1
    I don't know why people marks a question as a duplicate when the anwser of the duplicate are not the same as the question...people read a little before flag a question? – Mickaël Leger May 29 '18 at 08:51

1 Answers1

4

You can use array_count_values and array_column.

$counts = array_count_values(array_column($arr, "usr_name"));

This should give you an associative array with key being the name and the value being the count of that name.

Andreas
  • 23,610
  • 6
  • 30
  • 62