构建与QUOT;交叉"或QUOT;透视"从PHP中的数组表 [英] Building a "crosstab" or "pivot" table from an array in php
问题描述
我有同样的定义对象的数组如下:
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.
什么是在PHP中做到这一点的最有效方法是什么?
What's the most efficient way to do this in php?
推荐答案
如果我们可以假设:
- 分数的数组中的顺序始终是为了通过玩家的名字
然后由循环数 - 轮的数量是相同的,每个玩家
那么,我们能做的就是打印每名球员的得分,因为我们通过阵列移动在计算的过程中,但总复位,如果我们看到一个新的播放器:
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;
}
示例输出:
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.
这篇关于构建与QUOT;交叉&QUOT;或QUOT;透视&QUOT;从PHP中的数组表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!