1

这是我的代码:

if ( 0 < $matches->total() ) {
    while ( $matches->fetch() ) {
        ?>
            <?php $ma1_winner      = $matches->display( 'winner' ); ?>
            <?php $ma1_team_1      = $matches->display( 'team_1' ); ?> 
            <?php $matches_array_1['winner'] = $ma1_winner; ?>
                <?php $matches_array_1['team1'] = $ma1_team_1; ?>
            <?php
                    } // end of while loop
            } // end of if any exists
            ?>


            <?php var_dump($matches_array_1); ?>
            <?php die(); ?>

但它在 var_dump 中仅输出一个获胜者,而不是我数据库中的 15 个团队。如何解决?

4

2 回答 2

1

对于每次迭代,附加一个新数组,其中winnerteam作为其键。结果将是一个包含所有值的二维数组。

while ($matches->fetch() {
  // Append a new array via [] = array()
  // for every loop iteration
  $matches_array_1[] = array(
     'winner'=>$matches->display('winner'), 
     'team'=>$matches->display('team')
  );
}
var_dump($matches_array_1);

否则,您只是在每次迭代winner中覆盖相同的两个键。team

于 2012-10-11T18:07:31.283 回答
1

构建数组时,您需要为每个匹配项提供某种唯一匹配标识符。就像是:

<?
if ( 0 < $matches->total() ) 
{
    while ( $matches->fetch() ) 
    {
        $matches_array_1[$matches->display('match_id')]['winner'] = $matches->display( 'winner' );
        $matches_array_1[$matches->display('match_id')]['team1']  = $matches->display( 'team_1' );
    } // end of while loop
} // end of if any exists
var_dump($matches_array_1); 
die();
?>
于 2012-10-11T18:11:04.400 回答