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

The Archive Base Latest Questions

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

I have an array that represents soccer match results ( team1 – the name

  • 0

I have an array that represents soccer match “results” (team1 – the name represented by a number and team1_points that represents how many points this team earn from this match (3 for win, 1 for draw and 0 for lost).

Here is the code:

  array (size=9)
  0 => 
    array (size=2)
      'team1' => string '2' (length=1)
      'team1_points' => string '0' (length=1)
  1 => 
    array (size=2)
      'team1' => string '2' (length=1)
      'team1_points' => string '0' (length=1)
  2 => 
    array (size=2)
      'team1' => string '2' (length=1)
      'team1_points' => string '0' (length=1)
  3 => 
    array (size=2)
      'team1' => string '14' (length=2)
      'team1_points' => string '0' (length=1)
  4 => 
    array (size=2)
      'team1' => string '14' (length=2)
      'team1_points' => string '3' (length=1)
  5 => 
    array (size=2)
      'team1' => string '1' (length=1)
      'team1_points' => string '3' (length=1)
  6 => 
    array (size=2)
      'team1' => string '13' (length=2)
      'team1_points' => string '1' (length=1)
  7 => 
    array (size=2)
      'team1' => string '5' (length=1)
      'team1_points' => string '1' (length=1)
  8 => 
    array (size=2)
      'team1' => string '7' (length=1)
      'team1_points' => string '0' (length=1)

As you can see some of the team1 are repeating like ‘team1’ => ‘2’ is there 3 times and team with id=”14″ is there 2 times ‘team1′ => ’14’ etc.

I need to group the same teams based on team1 column (teams with the same id e.g. 2 or 14 etc. and create a new alias column called e.g. team_1_occurrences tht will hold the number of that team in the array and also a column e.g. team1_total_points that will SUM all the team1_points (3,1 or 0).

And then order by team1_total_points column DESC.

So, in the end I need something like this:

team1 | team1_occurrences | team1_total_points
----------------------------------------------
14    | 2                 | 3
----------------------------------------------
2     | 3                 | 0

…

My array created from while loop contains only 2 columns team1 and team1_points (the result is the var_dump in the beginning of my question:

$matches_array[] = array(
     'team1'=> $team_1_id,
     'team1_points'=> $team1_points
);

I guess I need somehow use the php function array_count_values and create a new column but I don’t know how.

Thanks in advance for any advice how to solve 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-12T16:19:39+00:00Added an answer on June 12, 2026 at 4:19 pm

    You can try

    $array = array(
            0 => array('team1' => '2','team1_points' => '1'),
            1 => array('team1' => '2','team1_points' => '1'),
            2 => array('team1' => '2','team1_points' => '0'),
            3 => array('team1' => '14','team1_points' => '0'),
            4 => array('team1' => '14','team1_points' => '3'),
            5 => array('team1' => '1','team1_points' => '3'),
            6 => array('team1' => '13','team1_points' => '1'),
            7 => array('team1' => '5','team1_points' => '1'),
            8 => array('team1' => '7','team1_points' => '1'));
    
    
    
    $list = array();
    
    array_map(function($var){}, $array);
    foreach ( $array as $value ) {
        $key = $value['team1'];
    
        if (array_key_exists($key, $list)) {
            $list[$key]['team1_points'] += $value['team1_points'];
            $list[$key]['team_1_occurrences'] ++;
        } else {
            $list[$key] = $value;
            $list[$key]['team_1_occurrences'] = 1;
        }
    }
    
    usort($list ,function($a, $b){ $a = $a['team1_points'] ; $b = $b['team1_points'] ; return ($a == $b) ? 0 : (($a < $b) ? 1 : -1 ) ;});
    var_dump($list);
    

    Output

    array
      0 => 
        array
          'team1' => string '14' (length=2)
          'team1_points' => int 3
          'team_1_occurrences' => int 2
      1 => 
        array
          'team1' => string '1' (length=1)
          'team1_points' => string '3' (length=1)
          'team_1_occurrences' => int 1
      2 => 
        array
          'team1' => string '2' (length=1)
          'team1_points' => int 2
          'team_1_occurrences' => int 3
      3 => 
        array
          'team1' => string '7' (length=1)
          'team1_points' => string '1' (length=1)
          'team_1_occurrences' => int 1
      4 => 
        array
          'team1' => string '13' (length=2)
          'team1_points' => string '1' (length=1)
          'team_1_occurrences' => int 1
      5 => 
        array
          'team1' => string '5' (length=1)
          'team1_points' => string '1' (length=1)
          'team_1_occurrences' => int 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a symmetric 2d array myMSTdata[][] of points that represents a minimum spanning
I have a program that accepts a number that represents an array index and
Say I have an array that represents a set of points: x = [2,
I have an int array that represents a very large number such as: //
Suppose I have an array that mimics a database table. Each array element represents
I have a an array of byte, size n, that really represents an array
I have a structure called Patch that represents a 2D array of data. newtype
I have an array that represents a grid For the sake of this example
I have an 3 dimensional array that represents an xy grid, and the z
I am using a class called BigNumDesc that represents a number. I have a

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.