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

  • SEARCH
  • Home
  • 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 8370405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:54:15+00:00 2026-06-09T13:54:15+00:00

I have an array of people that looks like this: $people = Array (

  • 0

I have an array of people that looks like this:

$people =
    Array
    (
        [0] => Array
            (
                [email] => NameSurname@example.com
                [name] => Name Surname
                [count] => 0
            )

        [1] => Array
            (
                [email] => Name2Surname@example.com
                [name] => Name2 Surname
                [count] => 0
            )
    )

And I have an array which is the result of a MySQL query that looks like this:

$query=
    Array
    (
        [0] => Array
            (
                [email] => NameSurname@example.com
                [name] => Name Surname
            )

        [1] => Array
            (
                [email] => Name2Surname@example.com
                [name] => Name2 Surname
            )

        [2] => Array
            (
                [email] => NameSurname@example.com
                [name] => Name Surname
            )

    )

For each e-mail address in $people, I’d like $people['count'] to equal how many times that e-mail address occurs in $query.

I’ve tried loads of ways to do this, and I’m not quite getting the desired result.

For the avoidance of doubt, my end result based on the example above should look like:

$people =
    Array
    (
        [0] => Array
            (
                [email] => NameSurname@example.com
                [name] => Name Surname
                [count] => 2
            )

        [1] => Array
            (
                [email] => Name2Surname@example.com
                [name] => Name2 Surname
                [count] => 1
            )
    )
  • 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-09T13:54:16+00:00Added an answer on June 9, 2026 at 1:54 pm
    foreach ($people as $key => $man) { // iterating through each man
      $_occurences = 0;
    
      foreach ($query as $_item) // iterating through each result in the result set
        if ($_item['email'] == $man['email']) // comparing current man to each result item
          $_occurences ++;
    
      $people[$key]['count'] = $_occurences; // saving number of occurrences in the `count` key 
    
     }
    

    UPD: One more solution using array_map, array_reduce and nice ternary operator. Its slower than one with foreach, but twice as compact and professional.

    It is slower (in terms of performance) bec. of function calls overhead, but on “small” amount of iterations this drop is negligible. It will update $people array without reassigning it bec. we transmitted each &$man by reference.

    array_map(function (&$man) use ($query) {
        $man['count'] = array_reduce($query, function ($count, $row) use ($man) {
            return ($row['email'] === $man['email']) ? ++$count : $count ;
        }, 0);
    }, $people);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL Database That Looks like This: Name: Joe Items: 25 CreationDate:
I have 2 arrays in PHP that look something like this: $rows = array(11,12,14,14,11,13,12,11);
I have a class that should look something like this: class Family_Type1 @people =
I have an array that is produced from people wanting to reserve a time
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
If I have an array that contains multiple FLV files like so: my @src_files
i have an array like this: [2011-06-16 16:37:20,23.2], [2011-06-21 16:37:20,35.6], [2011-06-26 16:37:20,41.8], [2011-07-01 16:37:20,25],
Alright, I've got an array that looks like. [0] => Array ( first_name =>
I have two different arrays. One array, a, for a list of people. My

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.