Christian T.
Christian T.

Reputation: 39

How to access keys and values of a multidimensional array

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,

Upvotes: 0

Views: 414

Answers (7)

swap
swap

Reputation: 282

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

Upvotes: 0

DurtyFree
DurtyFree

Reputation: 79

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

Upvotes: 0

Ranjit Shinde
Ranjit Shinde

Reputation: 1130

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

    }
}

Upvotes: 0

Geoff Atkins
Geoff Atkins

Reputation: 1703

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.

Upvotes: 0

Gwendal
Gwendal

Reputation: 1273

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

Upvotes: 0

Mr. Engineer
Mr. Engineer

Reputation: 3515

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

Upvotes: 0

Jon Surrell
Jon Surrell

Reputation: 9647

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

Upvotes: 1

Related Questions