PHP数组中的动态表

时间:2014-04-11 11:45:50

标签: php dynamic

我正在尝试将多维php数组显示为表格...有人可以帮忙吗?

这是我的阵列:

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [c1] => UA07
                    [s1] => 6
                    [c2] => Ultimate Force
                    [s2] => 8
                )

            [1] => Array
                (
                    [c1] => UF HEROES
                    [s1] => 6
                    [c2] => OLD School
                    [s2] => 4
                )

            [2] => Array
                (
                    [c1] => Winners 05 
                    [s1] => not_played
                    [c2] => World XI
                    [s2] => not_played
                )

            [3] => Array
                (
                    [c1] => Outlaw
                    [s1] => 4
                    [c2] => UWK
                    [s2] => 3
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [c1] => Ultimate Force
                    [s1] => 2
                    [c2] => UF HEROES
                    [s2] => 4
                )

            [1] => Array
                (
                    [c1] => BY
                    [s1] => 0
                    [c2] => Outlaw
                    [s2] => 0
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [c1] => UF HEROES
                    [s1] => 5
                    [c2] => Outlaw
                    [s2] => 1
                )

        )

)

这个阵列目前包含3轮,但理论上可以包含更多......并且在每一轮中它包含每个游戏,并且在每个游戏中它包含那些游戏的结果......

C1 / C2是竞争对手,S1 / S2是得分......

如何在列中显示每个数组并转到下一个数组并在列中显示它们......将它们排成一个比赛支架

任何想法都会非常有用。

我正在尝试实现这样的格式:

http://new.playdat.com/tournament-results.php" Bracket"

2 个答案:

答案 0 :(得分:0)

正如其他人在评论中所说,你需要一个嵌套循环。由于您没有指定表格格式,我只是创建了一个示例。您可以在http://writecodeonline.com/php/

运行此功能
/** Simulating your data **/
$rounds = array();
$rounds[1][] = array('c1' => 'UA07', 's1' => 6, 'c2' => 'Ultimate Force', 's2' => 8); 
$rounds[1][] = array('c1' => 'UF HEROES', 's1' => 6, 'c2' => 'Old School', 's2' => 4); 
$rounds[2][] = array('c1' => 'Ultimate Force', 's1' => 2, 'c2' => 'UF HEROES', 's2' => 4); 
$rounds[2][] = array('c1' => 'BY', 's1' => 0, 'c2' => 'Outlaw', 's2' => 0); 


$table = <<<EOD

        <table>
            <thead>
                <tr>
                    <th>Round</th>
                    <th>C1</th>
                    <th>S1</th>
                    <th>C2</th>
                    <th>S2</th>
                </tr>
            </thead>
            <tbody>
EOD;

foreach ($rounds as $roundNum=> $round){

    foreach ($round as $game) {

        $table .= "<tr>";
        $table .= "<td>{$roundNum}</td>";
        $table .= "<td>{$game['c1']}</td>";
        $table .= "<td>{$game['s1']}</td>";
        $table .= "<td>{$game['c2']}</td>";
        $table .= "<td>{$game['s1']}</td>";
        $table .= "</tr>";


    }
}

$table .= <<<EOD
            </tbody>
        </table>
EOD;

echo $table;

答案 1 :(得分:0)

动态锦标赛支架:

你的阵列:

$rounds = array(
    //Round 1
    array(

        array(
            'c1' => 'UA07 ',
            's1' => 6 ,
            'c2' => 'Ultimate Force ',
            's2' => 8 ,
        ),

        array(
            'c1' => 'UF HEROES',
            's1' => 6,
            'c2' => 'OLD School',
            's2' => 4,
        ),

        array(
                'c1' => 'Winners 05 ',
                's1' => 'not_played',
                'c2' => 'World XI',
                's2' => 'not_played',
        ),

        array(
                'c1' => 'Outlaw',
                's1' => 4,
                'c2' => 'UWK',
                's2' => 3,
        ),

    ),
    //Round 2
    array(
       array(
                'c1' => 'Ultimate Force',
                's1' => 2,
                'c2' => 'UF HEROES',
                's2' => 4,
        ),

        array(
                'c1' => 'BY',
                's1' => 0,
                'c2' => 'Outlaw',
                's2' => 0,
        ),

    ),
    //Round 3
    array(
        array(
                'c1' => 'UF HEROES',
                's1' => 5,
                'c2' => 'Outlaw',
                's2' => 1,
        ),

    )
);

发电机:

$roundCount = 1;
$totalRounds = count($rounds);

echo '<table border="1"><tr>';

for($i = 1;$i <= $totalRounds;$i++)
{
    echo '<td>Round'.$i.'</td>';
}
echo '</tr><tr>';
foreach($rounds as $round)
{
    $matches = count($round);
    echo '<td>';


    foreach($round as $match)
    {

        echo $match['c1'].' - '.$match['s1'].'<br>';
        echo $match['c2'].' - '.$match['s2'].'<br><hr>';

    }
    $roundCount++;
    echo '</td>';
}

echo '</tr></table>';

结果:

tournament bracket 发电机2:

$roundCount = 1;
$totalRounds = count($rounds);

echo '<table border="1"><tr>';

for($i = 1;$i <= $totalRounds;$i++)
{
    echo '<td>Round'.$i.'</td>';
}
echo '</tr><tr>';
foreach($rounds as $round)
{
    $matches = count($round);
    echo '<td>';


    foreach($round as $match)
    {
        echo '<table border="1">';
        echo '<tr><td>'.$match['c1'].'</td><td>'.$match['s1'].'</td></tr>';
        echo '<tr><td>'.$match['c2'].'</td><td>'.$match['s2'].'</td></tr>';
        echo '</table>';
    }
    $roundCount++;
    echo '</td>';
}

echo '</tr></table>';

获胜者的发电机:

$roundCount = 1;
$totalRounds = count($rounds);

echo '<table border="1"><tr>';

for($i = 1;$i <= $totalRounds;$i++)
{
    echo '<td>Round'.$i.'</td>';
}
echo '<td>Winner</td>';
echo '</tr><tr>';
foreach($rounds as $round)
{
    $matches = count($round);
    echo '<td>';

    if($roundCount == $totalRounds)
    {
        if($round[0]['s1'] > $round[0]['s2']) {
            $finalist = array($round[0]['c1'],$round[0]['s1']);
        } else {
            $finalist = array($round[0]['c2'],$round[0]['s2']);
        }
    }

    foreach($round as $match)
    {
        echo '<table border="1">';
        echo '<tr><td>'.$match['c1'].'</td><td>'.$match['s1'].'</td></tr>';
        echo '<tr><td>'.$match['c2'].'</td><td>'.$match['s2'].'</td></tr>';
        echo '</table>';
    }
    $roundCount++;

    echo '</td>';
}
echo '<td>';

echo '<table border="1">';
echo '<tr><td>'.$finalist[0].'</td><td>'.$finalist[1].'</td></tr>';
echo '</table>';

echo '</td>';

echo '</tr></table>';