数组foreach循环

时间:2016-02-08 12:14:36

标签: php arrays

我尝试用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, 

它对我有用:

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

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

7 个答案:

答案 0 :(得分: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 .', ';
    }
}

答案 1 :(得分:0)

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

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

请参阅此链接,详细了解foreachhttp://php.net/manual/en/control-structures.foreach.php

答案 2 :(得分:0)

请改用:

<asp:Button ID="backbtn" runat="server"  Text="Go Back" OnClientClick="CallConfirmBox('<%= TextBox2.ClientId %>)';" onclick="btnback_click" />
<br/> 
<script type="text/javascript">
            function CallConfirmBox(id) {
                var usrresponse;
                alert("in call confirm box called");
                if ('<%=this.test%>') {

                    var usrresponse = window.confirm("Do you want to go back without saving the data");
                }
                if (usrresponse) //userresponse is true
                {
                    alert(usrresponse);
                   window.location = window.location.href;
                }
                else
               {
                  var e  =document.getElementById(id).getAttribute("value",true);
                    alert(e)
                }
            }
     </script>

请注意,我删除了<?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); } var。 $test自己迭代所有数组,因此不需要密钥!密钥用于foreachfor循环

答案 3 :(得分:0)

由于数组的多维特性,尝试简单地回显该值将无效。

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

这将成功遍历每个子数组,并在user_factions之间包含换行符。

答案 4 :(得分: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 .', ';
        }

    }
}

答案 5 :(得分: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的键

希望这有帮助

答案 6 :(得分: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>';
   }
相关问题