将两个数组放入一个表中。第一个阵列垂直,第二个水平

时间:2014-05-17 06:19:21

标签: php html arrays html-table

Name Bob   Jim   Moe   Rob
ID   555   666   777   888
Lvl  1     2     3     4

这是数组的一部分:

Array
(
    [heroes] => Array
        (
            [0] => Array
                (
                    [paragonLevel] => 384
                    [name] => Barbecue
                    [id] => 35335691
                    [level] => 70
                    [hardcore] => 
                    [gender] => 0
                    [dead] => 
                    [class] => barbarian
                    [last-updated] => 1400233350
                )

            [1] => Array
                (
                    [paragonLevel] => 384
                    [name] => Ethereal
                    [id] => 43477852
                    [level] => 70
                    [hardcore] => 
                    [gender] => 1
                    [dead] => 
                    [class] => crusader
                    [last-updated] => 1400283921
                )

[这要达到8.我希望paragonlevel,name.id等在第一条垂直线上。然后我希望下一列填充字符数据,下一列填充下一个char等等]

名称,ID和lvl位于表中的一个数组中。如你所见,它们是垂直部分。 现在为" name"你在水平线上看到一些名字..来自第二个数组。

目前我可以填充垂直线..但我似乎无法填充水平右侧。

$herokeys   = array_keys($CAREER_DATA["heroes"][0]);
echo "<table width='700' border='5' summary='Table for Testing.'><caption id='bhcc'>Basic Hero Chart ($para)</caption>";
foreach(array_slice($herokeys, 1) as $herokey) {
   $herokey = ucwords($herokey);
   echo "<tr>";
   echo "<th id='RowTitle' scope='row'>$herokey</th>";
   foreach($CAREER_DATA["heroes"] as $i => $hero) {
      $name   = $CAREER_DATA["heroes"][$i]['name'];
      echo "<th id='chname' scope='col'>$name</th>";
   }
   echo "</tr>";
echo "</table>";

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您可以使用foreach将其设为垂直格式。考虑这个例子:

<?php
$values_from_db = array(
    'heroes' => array(
        array(
            'paragonLevel' => 384,
            'Name' => 'Barbeque',
            'id' => 35335691,
            'level' => 70,
            'hardcore' => '',
            'gender' => 0,
            'dead' => '',
            'class' => 'barbarian',
            'last-updated' => 1400233350,
        ),
        array(
            'paragonLevel' => 384,
            'Name' => 'Ethereal',
            'id' => 43477852,
            'level' => 70,
            'hardcore' => '',
            'gender' => 1,
            'dead' => '',
            'class' => 'crusader',
            'last-updated' => 1400283921,
        ),
        array(
            'paragonLevel' => 999,
            'Name' => 'GM',
            'id' => 999999999,
            'level' => 999,
            'hardcore' => 'yes',
            'gender' => 3,
            'dead' => '',
            'class' => 'god',
            'last-updated' => 1400233350,
        ),
    ),
);

// $keys = array_keys($values_from_db['heroes'][0]);
$keys = array('Name', 'id', 'level'); // needed keys
?>

<table border="1" cellpadding="10">
<?php foreach($keys as $value): ?>
<tr>
    <td style="background-color: yellow;"><?php echo $value; ?></td>
    <?php foreach($values_from_db as $index => $element): ?>
        <?php foreach($element as $k => $v): ?>
            <td><?php echo $v[$value]; ?></td>
        <?php endforeach; ?>
    <?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>