Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8782753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:39:06+00:00 2026-06-13T20:39:06+00:00

Say I have an array like this: [[bham, php], [auburn, php], [bham, php], [phoenix,

  • 0

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:

  1. Count the number of arrays that have php as the 2nd element – so `[“bham”, “php”] would count as 1.
  2. Count the number of times each of the first elements appear throughout the list. i.e. how many times does bham appear in the entire array, and how many times does auburn appear, 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:39:08+00:00Added an answer on June 13, 2026 at 8:39 pm

    Count is easy:

    a.count{|x| x[1] == 'php'}
    #=> 24
    

    And for grouping how about group_by:

    a.group_by{|x| x[0]}.map{|k,v| [k,v.size]}
    #=> [["bham", 2], ["auburn", 1], ["phoenix", 18], ["mobile", 1], ["tucson", 2]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an array like this: <?php $colors=array(); $colors[0]['id']=1; $colors[0]['color']='blue'; $colors[1]['id']=2; $colors[1]['color']='green'; ......
Say I have an array like this: [auburn, http://auburn.craigslist.org/web/, http://auburn.craigslist.org/cpg/, http://auburn.craigslist.org/eng/, http://auburn.craigslist.org/sof/, http://auburn.craigslist.org/sad/] What
In PHP, say that you have an associative array like this: $pets = array(
say I have an array in php like this $info['name'] = 'test %value%'; $info['city']
Say I have an array like this: [white, red, blue, red, white, green, red,
Let's say I have an array like this: $array = array (this,is,me,and,I,am,an,array,hello,beautiful,world); How can
Lets say I have an array with a structure like this: $arr= Array( array(
Say, I have an array that looks like this: var playlist = [ {artist:Herbie
Say I have an object-like data record like this: $article = array( 'title' =>
I have a 2d array, let's say like this : 2 0 8 9

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.