Say I have an array like this:
[["bham", "php"],
["auburn", "php"],
["bham", "php"],
["phoenix", "php"],
["phoenix", "php"],
["phoenix", "php"],
["phoenix", "php"],
["phoenix", "php"],
["mobile", "php"],
["phoenix", "php"],
["phoenix", "php"],
["phoenix", "php"],
["phoenix", "php"],
["phoenix", "php"],
["phoenix", "php"],
["phoenix", "php"],
["phoenix", "php"],
["phoenix", "php"],
["phoenix", "php"],
["tucson", "php"],
["tucson", "php"],
["phoenix", "php"],
["phoenix", "php"],
["phoenix", "php"]]
I would like to do a few things:
- Count the number of arrays that have
phpas the 2nd element – so `[“bham”, “php”] would count as 1. - Count the number of times each of the first elements appear throughout the list. i.e. how many times does
bhamappear in the entire array, and how many times doesauburnappear, etc. So basically, I want to cycle through this 2D array and for the first element of each child, I want to check to see if I have already recorded this string – if I have then I increment the value recorded and if I have not then I create a new entry for this new string.
This particular array is relatively trivial and can be done visually relatively easily, but assume that I will have an array with hundreds/thousands of elements.
It is also safe to assume that both elements of each child array will always be a single word – so it should be relatively easy to keep track of.
How do I approach this?
Count is easy:
And for grouping how about group_by: