Paul Duncan Paul Duncan - 1 month ago 13
PHP Question

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

Answer

You can use foreach to make it a vertical format. 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>
Comments