Christian Theiß February 2016

Array foreach loop

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, 

it works for me with:

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

for the hp:

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'];
}

Answers


Jon Surrell February 2016

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 .', ';
    }
}


Mr. Engineer February 2016

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


Gwendal February 2016

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


GeoffAtkins February 2016

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.


Ranjith February 2016

$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 .', ';
        }

    }
}


DurtyFree February 2016

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


swaroop suthar February 2016

  $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>';
   }

Post Status

Asked in February 2016
Viewed 1,376 times
Voted 10
Answered 7 times

Search




Leave an answer