0

I try to give out the user_factions array with subs. (this is a part of a bigger array). Manually it works! with:

echo $stat['user_factions'][0]['online'] 

I tried and 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] as $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, 
3
  • Try to re-organize the array into a new array. A loop over $stat['user_factions] will return the arrays one by one. Commented Feb 8, 2016 at 12:18
  • Do you want to reorganize the array or just display its contents? Commented Feb 8, 2016 at 12:23
  • just display the content. Commented Feb 8, 2016 at 12:48

7 Answers 7

1

You're trying to treat a foreach like a while loop. Since you have nested arrays, just use nested foreach:

This:

// You're statically picking a child // | // v foreach ($stat['user_factions'][$test] as $key => $value){ echo $key .' = '. $value .', '; $test++; // <-- This is having no effect. } 

Will become:

foreach ($stat['user_factions'] as $faction) { foreach ($faction as $key => $value) { echo $key .' = '. $value .', '; } } 
Sign up to request clarification or add additional context in comments.

1 Comment

it works! thanks a lot :) -> foreach ($stat['user_factions'] as $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']; }
0

Change your foreach from

foreach ($stat['user_factions'][$test] as $key => $value) { 

To

foreach ($stat['user_factions'] as $key => $value){ 

Please refer this link to know more about foreach : http://php.net/manual/en/control-structures.foreach.php

Comments

0

Use this instead :

<?php $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, ), ), ); foreach ($stat['user_factions'] as $key => $value){ echo $key.": "; var_dump($value); } 

Notice that I removed $test var. foreach iterates all the array by itself, so no need for a key ! Keys are use for for and while loops

Comments

0

Because of the multidimensional nature of your arrays, trying to simply echo the value won't work.

foreach ($stat['user_factions'] as $sub_array){ foreach ($sub_array as $key => $value) { echo $key .' = '. $value .', '; } echo '<br>'; } 

This will iterate through each sub array successfully, and include the line break between user_factions.

Comments

0
$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, ), ), ); foreach ($stat['user_factions'] as $key => $value){ if(is_array($value)) { foreach($value as $key=>$value) { echo $key .' = '. $value .', '; } } } 

Comments

0

Use nested foreach loops like this:

$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, ), ), ); foreach ($stat['user_factions'] as $key => $values) { echo $key, "<br />"; foreach ($values as $property => $value) { echo $property, "=", $value, "<br />"; } } 

The output is something like:

0 online=30 planets=185 registred=138 registred_today=1 1 online=35 planets=210 registred=175 registred_today=0 

$key represents the user_factions index number, $property the key to the regarding $values

Hope this helps

Comments

0
 $test=0; foreach ($stat['user_factions'] as $pr_key => $pr_value){ echo $pr_value .' = '. $test .', '; foreach ($stat['user_factions'][$pr_key] as $ch_key => $ch_value){ echo $ch_key .' = '. $ch_value .', '; } $test++; echo '<br>'; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.