我有一个类似于以下定义的对象数组:
$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;
如果我遍历并将$scores
对象转储到表中,它将如下所示:
Round_Name 玩家得分 ---------------------------- 第 1 轮鲍勃 10 第 2 轮鲍勃 7 第一轮杰克 6 回合 2 杰克 12
然而,我想要的是这样的:
玩家 第 1 轮 第 2 轮总计 ------------------------------------------- 鲍勃 10 7 17 杰克 6 12 18
我不会提前知道会有多少回合或玩家,让我们说我不能改变对象的构造方式。
在 php 中执行此操作的最有效方法是什么?