php - Array foreach loop -
i try give out user_factions
array subs. (this part of bigger array). manually works! with:
echo $stat['user_factions'][0]['online']
i tried , failed:
$stat = array ( 'user_factions' => array ( 0 => array ( 'online' => 30, 'planets' => 185, 'registred' => 138, 'registred_today' => 1, ), 1 => array ( 'online' => 35, 'planets' => 210, 'registred' => 175, 'registred_today' => 0, ), ), ); $test= 0; foreach ($stat['user_factions'][$test] $key => $value){ echo $key .' = '. $value .', '; $test++; }
only got this:
online = 30, planets = 185, registred = 138, registred_today = 1,
i want:
user_factions = 0, online = 30, planets = 185, registred = 138, registred_today = 1, user_factions = 1, online = 35, planets = 210, registred = 175, registred_today = 0,
it works me with:
foreach ($stat['user_factions'] $faction) { foreach ($faction $key => $value) { echo $key .' = '. $value .', '; } }
for hp:
foreach ($stat['user_factions'] $key => $value){ echo '<br/>faction: '. $key .'<br/> online: '. $value["online"] .'<br/> planets: ' . $value['planets'] .'<br/> registered players: ' . $value['registred'] .'<br/> new players today: ' . $value['registred_today']; }
you're trying treat foreach
while
loop. since have nested arrays, use nested foreach:
this:
// you're statically picking child // | // v foreach ($stat['user_factions'][$test] $key => $value){ echo $key .' = '. $value .', '; $test++; // <-- having no effect. }
will become:
foreach ($stat['user_factions'] $faction) { foreach ($faction $key => $value) { echo $key .' = '. $value .', '; } }
Comments
Post a Comment