如何访问多维数组的键和值

问题描述 投票:0回答:7

我尝试用 subs 给出

user_factions
数组。 (这是一个更大数组的一部分)。 手动就可以了!与:

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

我尝试过但失败了:

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

只得到这个:

online = 30, planets = 185, registred = 138, registred_today = 1, 

我想要:

user_factions = 0, online = 30, planets = 185, registred = 138, registred_today = 1, 

user_factions = 1, online = 35, planets = 210, registred = 175, registred_today = 0,
php arrays
7个回答
1
投票

您试图将

foreach
视为
while
循环。由于您有嵌套数组,因此只需使用嵌套 foreach 即可:

这个:

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

将成为:

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

0
投票

更改你的 foreach

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

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

请参阅此链接以了解更多有关

foreach
的信息:http://php.net/manual/en/control-structs.foreach.php


0
投票

用这个代替:

<?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);
}

请注意,我删除了

$test
var。
foreach
自行迭代所有数组,因此不需要键!按键用于
for
while
循环


0
投票

由于数组的多维性质,尝试简单地回显该值是行不通的。

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

这将成功迭代每个子数组,并包括

user_factions
之间的换行符。


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

    }
}

0
投票

像这样使用嵌套的 foreach 循环:

$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 />";
  }
}

输出类似于:

0
online=30
planets=185
registred=138
registred_today=1

1
online=35
planets=210
registred=175
registred_today=0

$key 代表 user_factions 索引号,$property 代表相关 $values 的键

希望这有帮助


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>';
   }
© www.soinside.com 2019 - 2024. All rights reserved.