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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:16:37+00:00 2026-06-12T21:16:37+00:00

I have 2 arrays $list and $list2 : $list : Array ( [0] =>

  • 0

I have 2 arrays $list and $list2:

$list:

Array
(
[0] => Array
    (
        [team] => 14
        [team_points] => 3
        [team_occurrences] => 2
    )

[1] => Array
    (
        [team] => 1
        [team_points] => 3
        [team_occurrences] => 2
    )

[2] => Array
    (
        [team] => 5
        [team_points] => 1
        [team_occurrences] => 1
    )

[3] => Array
    (
        [team] => 13
        [team_points] => 1
        [team_occurrences] => 1
    )

[4] => Array
    (
        [team] => 7
        [team_points] => 0
        [team_occurrences] => 1
    )

[5] => Array
    (
        [team] => 2
        [team_points] => 0
        [team_occurrences] => 3
    )

 )

$list2:

Array
(
    [0] => Array
        (
            [team] => 20
            [team_points] => 7
            [team_occurrences] => 3
        )

    [1] => Array
        (
            [team] => 10
            [team_points] => 3
            [team_occurrences] => 1
        )

    [2] => Array
        (
            [team] => 14
            [team_points] => 3
            [team_occurrences] => 1
        )

    [3] => Array
        (
            [team] => 13
            [team_points] => 3
            [team_occurrences] => 1
        )

    [4] => Array
        (
            [team] => 19
            [team_points] => 3
            [team_occurrences] => 1
        )

    [5] => Array
        (
            [team] => 17
            [team_points] => 1
            [team_occurrences] => 1
        )

    [6] => Array
        (
            [team] => 11
            [team_points] => 0
            [team_occurrences] => 1
        )

    [7] => Array
        (
            [team] => 15
            [team_points] => 0
            [team_occurrences] => 1
        )

)

As you can see the columns are the same in both arrays (team, team_points, team_occurrences)

Now, I would like to merge these two arrays into array called $list_all

The problem with the merge is that standard merge I have tried

array_merge($list,$list2);

just add them together.

However, What I need is to count same teams e.g. [team] => 14 and [team] => 13 are in both arrays (in $list and $list2) , so, therefore I need to sum team_points column value from $list with and team_points column value from $list2 if the team is identical. The same for team_occurrences column.

So e.g.

New array will not look like this:

Array
(
[0] => Array // from $list
    (
        [team] => 14
        [team_points] => 3
        [team_occurrences] => 2
    )

[1] => Array // from $list2
    (
        [team] => 14
        [team_points] => 3
        [team_occurrences] => 1
    )

[3] => Array // from $list
    (
        [team] => 13
        [team_points] => 1
        [team_occurrences] => 1
    )

[4] => Array // from $list2
    (
        [team] => 13
        [team_points] => 3
        [team_occurrences] => 1
    )

But I need that it will look like this:

Array
(
[0] => Array 
    (
        [team] => 14
        [team_points] => 6
        [team_occurrences] => 3
    )

[1] => Array 
    (
        [team] => 13
        [team_points] => 4
        [team_occurrences] => 2
    )

After the merge I would like to sort the result array using usort() or maybe some better function by team_points DESC (from highest value to the lowest).

Thanks in advance for any advice.

  • 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-12T21:16:38+00:00Added an answer on June 12, 2026 at 9:16 pm

    Iterate over the first list, creating a new array whose keys are the team numbers. Then iterating over the second array, if the team already exists, add and otherwise, append.

    $combined = array();
    // First loop creates array keys in $combined array
    // based on team numbers
    foreach($list as $initial) {
      $combined[$initial['team']] = $initial; 
    }
    
    // Second loop looks at $list2 and either adds to values
    // if the team key already exists, or just adds the 
    // whole subarray on if it doesn't.
    foreach($list2 as $secondary) {
      // If it was in the first, append to it.
      if (isset($combined[$secondary['team']])) {
        $combined[$secondary['team']]['team_points'] += $secondary['team_points'];
        $combined[$secondary['team']]['team_occurrences'] += $secondary['team_occurrences'];
      }
      // Otherwise, just add it to the array
      else {
        $combined[$secondary['team']] = $secondary;
      }
    }
    
    // Then sort the combined array with usort() into descending order by points
    // Anonymous callback function is PHP 5.3+. Needs to be a string value of a named function
    // for PHP < 5.3
    usort($combined, function($a, $b) {
      if ($a['team_points'] === $b['team_points']) {
         return 0;
      }
      else {
         return $a['team_points'] < $b['team_points'] ? 1 : -1;
      }
    });
    

    Note: The above produces an output array keyed by team number. If you really just want ascending numeric keys, call array_values() on it:

    // Strip off the team number keys and just use ascending numbers
    $combined = array_values($combined);
    

    Edit Fixed a few missing ] above…

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

Sidebar

Related Questions

I have two different arrays. One array, a, for a list of people. My
C++ Notes: Array Initialization has a nice list over initialization of arrays. I have
I have two arrays, each containing strings. The first array is a list of
I have an array list of objects in my application. private static ArrayList<Player> userList=new
I have an array list named newSymptomList which contains a list of Symptom id's
I have an array list of objects and I am using this example to
I have a collection (or list or array list) in which I want to
I have a list of Object arrays (List) That I am going to pass
I got requirements- 1. Have random values in a List/Array and I need to
I have a List with array of object values. I have to display value

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.