php多个表数据交叉显示,在php中从数组构建“交叉表”或“数据透视表”

如果我们可以假设:

>数组中得分的顺序始终按玩家姓名排序

然后按轮数

>每个玩家的回合数相同

然后,我们可以做的是在我们移动阵列时打印每个玩家的得分,同时计算过程中的总数,但如果我们看到一个新玩家则重置它:

$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

这应该非常有效,因为它只通过一次数组.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值