簡體   English   中英

將兩個數組合並到一個表中。 第一列垂直,第二列水平

[英]Two arrays into one table. First array vertical, second horizontal

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等位於第一個垂直線上。 然后,我要用字符數據填充下一列,並用下一個字符填充下一列,依此類推]

名稱,ID和lvl位於表中的一個數組中。 如您所見,它們是垂直部分。 現在,對於“名稱”,您會在水平線上看到一些名稱。

目前,我可以填充垂直線..但是我似乎無法填充水平線。

$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>";

我該怎么做呢?

您可以使用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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM