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 8839265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:12:04+00:00 2026-06-14T10:12:04+00:00

The array that I use is an array merged from three different arrays (3

  • 0

The array that I use is an array merged from three different arrays (3 different mysql results). Each array is an associative array with a few fields and the count field is the field I need.

The following piece of array is a double entry. I want to check the array and check if there are duplicates (id field) and calculate the sum of the count field. What would be the best-practice to solve this issue?

array(21) {
[2] => array(6) {
  ["id"] => string(2) "71"
  ["artist"] => string(7) "Arsenal"
  ["title"] => string(10) "Oyebo Soul"
  ["genres_id"] => string(2) "13"
  ["mbid"] => string(36) "0048d294-1557-4e05-8c82-8bc3f8f11923"
  ["count"] => string(1) "4"
}
[3] => array(6) {
  ["count"] => string(1) "3"
  ["id"] => string(2) "71"
  ["artist"] => string(7) "Arsenal"
  ["title"] => string(10) "Oyebo Soul"
  ["genres_id"] => string(2) "13"
  ["mbid"] => string(36) "0048d294-1557-4e05-8c82-8bc3f8f11923"
}

The result would be this:

[3] => array(6) {
  ["count"] => string(1) "7"
  ["id"] => string(2) "71"
  ["artist"] => string(7) "Arsenal"
  ["title"] => string(10) "Oyebo Soul"
  ["genres_id"] => string(2) "13"
  ["mbid"] => string(36) "0048d294-1557-4e05-8c82-8bc3f8f11923"
}

Hope this makes any sense. Thanks in advance

  • 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-14T10:12:05+00:00Added an answer on June 14, 2026 at 10:12 am

    There’s bound to be a ton of different ways to approach this problem, but the first one that popped into my head was to make use of array_reduce() on each set of "duplicate" albums:

    // Group and index the albums by 'mbid'...
    $indexed = array();
    
    foreach ($albums as $album) {
        $indexed[$album['mbid']][] = $album;
    }
    
    // Reduce all the album groups by simply totaling their 'count's...
    $indexed = array_map(function($albums) {
        return array_reduce($albums, function($a, $b) {
            $a['count'] += $b['count'];
            return $a;
        });
    }, $indexed);
    

    Note: This solution assumes that all the information in each album duplicate is identical except the count, as is the case in your example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a scheme that will heavily use array fields in mongo documents,
I want to take an array and use that array's values to populate an
I have an SHA-1 byte array that I would like to use in a
I need to create an array for time that I will use in a
I have set an array in my config file that I use global in
I have an array of strings that I want to use for button titles
I'm trying to use empty() in array mapping in php. I'm getting errors that
Let's say that I want to use C-style arrays of NSObjects instead of using
I'd like to merge two arrays with each other: $filtered = array(1 => 'a',
I've been learning some PHP and MySQL from a book that teaches you how

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.