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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:10:15+00:00 2026-05-11T14:10:15+00:00

I have an array of objects defined similarly to the below: $scores = array();

  • 0

I have an array of objects defined similarly to the below:

$scores = array();  // Bob round 1 $s = new RoundScore(); $s->Round_Name = 'Round 1'; $s->Player_Name = 'Bob'; $s->Score = 10; $scores[0] = $s;  // Bob round 2 $s = new RoundScore(); $s->Round_Name = 'Round 2'; $s->Player_Name = 'Bob'; $s->Score = 7; $scores[1] = $s;  // Jack round 1 $s = new RoundScore(); $s->Round_Name = 'Round 1'; $s->Player_Name = 'Jack'; $s->Score = 6; $scores[2] = $s;  // Jack round 2 $s = new RoundScore(); $s->Round_Name = 'Round 2'; $s->Player_Name = 'Jack'; $s->Score = 12; $scores[3] = $s; 

If I loop through and dump the $scores object into a table, it will look something like this:

 Round_Name   Player   Score ---------------------------- Round 1      Bob        10 Round 2      Bob         7 Round 1      Jack        6 Round 2      Jack       12 

What I want, however, is something like this:

 Player  Round 1  Round 2  Total ------------------------------- Bob       10        7       17 Jack       6       12       18 

I’m not going to know in advance how many rounds or players there’ll be and let’s just say I can’t change the way the objects are constructed.

What’s the most efficient way to do this in php?

  • 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. 2026-05-11T14:10:16+00:00Added an answer on May 11, 2026 at 2:10 pm

    If we can assume that:

    • the order of scores in the array is always in the order by player’s name and then by the round number
    • the number of rounds is same for each player

    Then, what we can do is print each player’s score as we moved through the array while calculating the total in the process but resetting it if we see a new player:

    $round_count = 0; $header_printed = false; $current_player = NULL; $current_total = 0; $current_output_line = ''; foreach ($scores as $score) {     // Check whether we have to move to a new player     if ($score->Player_Name != $current_player) {         // Check whether we have anything to print before         // resetting the variables         if (!is_null($current_player)) {             if (!$header_printed) {                 printf('%-10s', 'Player');                 for ($i = 0; $i < $round_count; $i++) {                     printf('%-10s', 'Round $i');                 }                 printf('%-10s\n', 'Total');                  $header_printed = true;             }              $current_output_line .= sprintf('%5d\n', $current_total);             print $current_output_line;         }          // Reset the total and various variables for the new player         $round_count = 0;         $current_player = $score->Player_Name;         $current_total = 0;         $current_output_line = sprintf('%-10s', $score->Player_Name);     }      $round_count++;     $current_total += $score->Score;     $current_output_line .= sprintf('%5d     ', $score->Score); } // The last player is not printed because we exited the loop  // before the print statement, so we need a print statement here. if ($current_output_line != '') {     $current_output_line .= sprintf('%5d\n', $current_total);     print $current_output_line; } 

    Sample output:

    Player    Round 0   Round 1   Total Bob          10         7        17 Jack          6        12        18 

    This should be quite efficient because it only goes through the array once.

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

Sidebar

Ask A Question

Stats

  • Questions 121k
  • Answers 121k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What if you really wanted to output something like <asp:xyz>?… May 12, 2026 at 12:31 am
  • Editorial Team
    Editorial Team added an answer I suggest you: OWASP Zed Attack Proxy Very complete and… May 12, 2026 at 12:31 am
  • Editorial Team
    Editorial Team added an answer PHP has no such thing as an ArrayCollection as far… May 12, 2026 at 12:31 am

Related Questions

Suppose I have an array of a objects of user defined class. Wanted to
Problem: I have a set of images, lets say 5. There is an structure
I have an array of java objects. Each object stores two longs that define
I'm developing on the iPhone and I'm missing something about storing objects in an

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.