问题
Basically this makes and fills out a table. It mostly does this great, except the foreach
seems to be making a new column for EVERY subarray, and I only want a column made for each in [heroes]
. My limited knowledge of php has forced me to come here again. I tried array_slice to try and get that to work (its at 0 now, but I tried other numbers). I also tried $CAREER_DATA['heroes']
in the foreach
but that just made it all worse.
Not sure how to get just the part I want out of this.
e: I just realized i can perhaps extract just that subarray out and have it as its own array and use a veriuable for that. Ill try that now... Id still like to know how to do it this way though.
Full array
$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>
回答1:
The better way to handle this is to extract the ['heroes']
sub-array and reformat it to fit the structure you ultimately intend to use as output. That basically means flipping it on its side such that each row relates to an original key.
There are a couple of ways to handle this, but since all your hero sub-arrays have the same keys, you can build a new sideways array with a loop, then use a pair of foreach
loops for your final output.
$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);
Now the structure of $output_array
looks more similar to your output table. Use a series of loops to write it out.
<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>
It can also be done using something more similar to your array_keys()
attempt without first reformatting the array. Call array_keys()
to get the property names, then loop the sub-arrays. This results in less code, but without the niceness of a data structure formatted how you want it in the first place.
<?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>
I'll recommend you use a CSS class instead of an inline style
for the property names. Instead of:
<td style="background-color: #101210;"><?php echo $key; ?></td>
Add a class like:
<td class='propname'><?php echo $key; ?></td>
and define it in your CSS
.propname { background-color: #101210; }
Lastly, the reason for your extra columns looks to be an incorrect use of array_slice(). Since you passed the main array $CAREER_DATA
and used 0
as the offset without providing the 3rd parameter $limit
, you end up iterating over the entire $CAREER_DATA
array with all its keys, effectively turning the array_slice()
into a full foreach
.
回答2:
First of all just like you did with the array_keys, get the values with array_values(.) and loop through them,
also, to get the first element of an array it's better to use
current($arr)
来源:https://stackoverflow.com/questions/23717041/multidimentional-array-foreach-over-first-sub-array-only