[英]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.