简体   繁体   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

This is (part of) the array: 这是数组(的一部分):

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
                )

[This goes upto 8. I want paragonlevel,name.id etc to be on the first vertical line. [这上升到8。我希望paragonlevel,name.id等位于第一个垂直线上。 Then I want the next column being filled with the character data, and the next column with the next char and so on] 然后,我要用字符数据填充下一列,并用下一个字符填充下一列,依此类推]

Name, ID and lvl are in one array in the table. 名称,ID和lvl位于表中的一个数组中。 As you see they are the vertical part. 如您所见,它们是垂直部分。 Now for "name" you see some names on the horizontal line.. thats from the second array. 现在,对于“名称”,您会在水平线上看到一些名称。

Currently I can populate the vertical line.. but I cant seem to populate the horizontal right. 目前,我可以填充垂直线..但是我似乎无法填充水平线。

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

How do I do this? 我该怎么做呢?

You can use foreach to make it a vertical format. 您可以使用foreach使其成为垂直格式。 Consider this example: 考虑以下示例:

<?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.

相关问题 PHP 将两个数组合并到一个表中。 第一个阵列垂直和水平 - PHP Two Arrays Into One Table. First Array Vertical & Horizontal PHP将两个数组合并为一个表。 第一阵列垂直,第二水平 - PHP Two Arrays Into One Table. First Array Vertical, Second Horizontal 合并两个数组,用第二个数组覆盖第一个数组 - Merging two arrays, overwriting first array with second one 两个数组,一个数组列出项目,第二个对第一个进行排序,php代码如何? - Two arrays, one array to list items, second to sort the first one, php code how to? 如何通过仅接管与第一个具有相同键的第二个数组中的值来合并两个 arrays? - How to merge two arrays by taking over only values from the second array that has the same keys as the first one? 排序两个数组,以便第二个数组排序基于第一个 - Sorting two arrays so that the second array sorting is based on the first PHP 比较两个 arrays 但第一个数组的所有值都需要在第二个 - PHP compare two arrays but all values of first array needs to be in second 仅获取组合在一个数组中的两个数组中的第一个 - Get only first of two arrays which are combined in one array PHP使用两个数组:第一个按值,第二个按键(基于第一个数组顺序) - PHP usort two arrays: first by value, second by key based on first array order 首先根据值合并两个数组,然后根据key-php合并第二个数组 - Merge two arrays first one based on value and second one based on key - php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM