基本上这会制作并填写表格。它主要做得很好,除了foreach
似乎为每个子阵列创建一个新列,我只想在[heroes]
中为每个列创建一个列。我对php的知识有限,迫使我再次来到这里。我尝试使用array_slice尝试使其工作(现在它为0,但我尝试了其他数字)。我还在$CAREER_DATA['heroes']
中尝试了foreach
,但这让事情变得更糟。
不确定如何获得我想要的部分。
e:我刚刚意识到我可能只是将该子阵列提取出来并将其作为自己的数组并使用可用的数据。我试试吧......我仍然想知道如何这样做。
完整数组
$CAREER_DATA = 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] => 1400357611
)
[2] => Array
(
[paragonLevel] => 384
[name] => Aikido
[id] => 35213628
[level] => 70
[hardcore] =>
[gender] => 1
[dead] =>
[class] => monk
[last-updated] => 1400186566
)
[3] => Array
(
[paragonLevel] => 384
[name] => Euphoria
[id] => 45715169
[level] => 70
[hardcore] =>
[gender] => 1
[dead] =>
[class] => demon-hunter
[last-updated] => 1399516194
)
[4] => Array
(
[paragonLevel] => 384
[name] => Entropy
[id] => 47278203
[level] => 70
[hardcore] =>
[gender] => 1
[dead] =>
[class] => wizard
[last-updated] => 1399595333
)
[5] => Array
(
[paragonLevel] => 384
[name] => Eulogy
[id] => 47138988
[level] => 11
[hardcore] =>
[gender] => 0
[dead] =>
[class] => witch-doctor
[last-updated] => 1400179777
)
[6] => Array
(
[paragonLevel] => 0
[name] => lolwut
[id] => 47160915
[level] => 6
[hardcore] => 1
[gender] => 0
[dead] =>
[class] => barbarian
[last-updated] => 1398282569
)
[7] => Array
(
[paragonLevel] => 384
[name] => MuleWeapons
[id] => 47148207
[level] => 1
[hardcore] =>
[gender] => 0
[dead] =>
[class] => crusader
[last-updated] => 1399596544
)
[8] => Array
(
[paragonLevel] => 384
[name] => MuleJunk
[id] => 47154043
[level] => 1
[hardcore] =>
[gender] => 0
[dead] =>
[class] => demon-hunter
[last-updated] => 1399596678
)
)
[lastHeroPlayed] => 43477852
[lastUpdated] => 1400357611
[kills] => Array
(
[monsters] => 1296806
[elites] => 33006
[hardcoreMonsters] => 241
)
[timePlayed] => Array
(
[barbarian] => 1
[crusader] => 0.261
[demon-hunter] => 0.035
[monk] => 0.278
[witch-doctor] => 0.017
[wizard] => 0.119
)
[fallenHeroes] => Array
(
)
[paragonLevel] => 384
[paragonLevelHardcore] => 0
[battleTag] => Paultimate#1333
[progression] => Array
(
[act1] => 1
[act2] => 1
[act3] => 1
[act4] => 1
[act5] => 1
)
)
PHP / HTML:
<? $herokeys = array_keys($CAREER_DATA["heroes"][0]);?>
<table border="1" cellpadding="3">
<?php foreach($herokeys as $value): ?>
<tr>
<td style="background-color: #101210;"><?php echo $value; ?></td>
<?php foreach(array_slice($CAREER_DATA, 0) as $index => $element): ?>
<?php foreach($element as $k => $v): ?>
<td><?php echo $v[$value]; ?></td>
<?php endforeach; ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
答案 0 :(得分:2)
处理此问题的更好方法是提取['heroes']
子数组并重新格式化以适合您最终打算用作输出的结构。这基本上意味着将其翻转到每侧行与原始密钥相关。
有几种方法可以解决这个问题,但是由于你的所有英雄子阵列都有相同的键,你可以构建一个带循环的新的侧向数组,然后使用一对foreach
循环最终输出。
$output_array = array();
// Start with just the heroes...
$heroes = $CAREER_DATA['heroes'];
// Looping over all of them...
foreach ($heroes as $hero) {
// And looping over each key to flip it:
foreach ($hero as $key => $value) {
// Initialize a new array for this key if it isn't already.
if (!isset($output_array[$key])) {
$output_array[$key] = array();
}
// Append the value
$output_array[$key][] = $value;
}
}
// See what it looks like now:
print_r($output_array);
现在$output_array
的结构看起来与输出表更相似。使用一系列循环来写出来。
<table border="1" cellpadding="3">
<?php foreach ($output_array as $key => $values): ?>
<tr>
<!--
$output_array is indexed by hero property, here as $key
so write that out first
-->
<td style="background-color: #101210;"><?php echo $key; ?></td>
<!-- then a loop for each of the values for that property -->
<?php foreach ($values as $value): ?>
<td><?php echo $value; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
也可以使用与array_keys()
尝试更相似的东西来完成,而无需先重新格式化阵列。调用array_keys()
获取属性名称,然后循环子数组。这样可以减少代码,但是没有数据结构的优点,无需首先按照您的需要进行格式化。
<?php $properties = array_keys($CAREER_DATA['heroes'][0]); ?>
<table border="1" cellpadding="3">
<?php foreach ($properties as $property): ?>
<tr>
<td style="background-color: #101210;"><?php echo $property; ?></td>
<!-- then a loop for each individual in heroes -->
<?php foreach ($CAREER_DATA['heroes'] as $hero): ?>
<!-- write out this individual's value for $property -->
<td><?php echo $hero[$property]; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
我建议您使用CSS类而不是内联style
作为属性名称。而不是:
<td style="background-color: #101210;"><?php echo $key; ?></td>
添加一个类:
<td class='propname'><?php echo $key; ?></td>
并在CSS
中定义它.propname { background-color: #101210; }
最后,您的额外列的原因似乎是对array_slice()
的错误使用。由于您传递了主数组$CAREER_DATA
并使用0
作为偏移而未提供第3个参数$limit
,因此您最终会使用其所有键遍历整个$CAREER_DATA
数组,有效地将array_slice()
变为完整foreach
。
答案 1 :(得分:0)
首先,就像使用array_keys一样,使用array_values(。)获取值并循环遍历它们,
另外,要获得数组的第一个元素,最好使用
current($arr)