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.

12
  • 2
    Does this help? stackoverflow.com/q/4948946 Commented May 29, 2018 at 8:37
  • 2
    Not the downvoter but possibly downvoted because this is a commonly asked question Commented May 29, 2018 at 8: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. Commented May 29, 2018 at 8: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 Commented May 29, 2018 at 8:40
  • 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? Commented May 29, 2018 at 8:51

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Don't know why this was downvoted.
Perfect, thank you
who is the intelligent person downvoted the answer
@Pogrindis probably because someone didn't like the question. It seems to happen quite often, someone just clicks all the downbuttons there is without reading.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.