0

I have the following array, and would like to count all instances of the inner array.

Array ( 
     [0] => Array ( [0] => b [1] => d) 
     [1] => Array ( [0] => c [1] => a) 
     [2] => Array ( [0] => b [1] => d)
     [3] => Array ( [0] => a [1] => d)
     )

I am looking at a count so that one would get:

2 of b,d

1 of c,a

1 of a,d

The outer array foreach to get get to the inner array to be done first. Not sure what is the approach in counting unique array values. Thank you.

1 Answer 1

2
function count_pairs($array) {
    foreach ($array as &$pair) {
        $pair = implode(', ', $pair);
    }
    return array_count_values($array);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Any reason why you would use "&$pair" and not just "$pair"?
Without &, $array won't be changed.
FatMachine's comment indicates why every answer should include an educational explanation.

Your Answer

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.