【发布时间】:2010-10-17 06:22:36
【问题描述】:
我有一个与下面类似的对象数组:
$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 对象并将其转储到表中,它将如下所示:
然而,我想要的是这样的:
玩家 第 1 轮 第 2 轮总计 ------------------------------------------- 鲍勃 10 7 17 杰克 6 12 18我不会提前知道会有多少轮或多少玩家,我只能说我无法改变对象的构造方式。
在 php 中最有效的方法是什么?
【问题讨论】:
-
我认为你有一个错字。对于 Bob 第 2 轮,您有:“$rounds[1] = $s;”代替:“$scores[1] = $s;”
-
谢谢,你是对的 - 我现在已经修好了 :)
标签: php arrays performance pivot-table