PHP通过多维数组循环

时间:2015-02-26 06:19:20

标签: php multidimensional-array

嗨,我在PHP中有这个多维数组:

$team_arrays = array (
                        "lakers" => array (
                                        24 => "Bryant",
                                        6  => "Price",
                                        17 => "Lin"
                        ),

                        "knicks" => array (
                                        7 => "Anthony",
                                       22 => "Shumpert",
                                       12 => "Jackson"
                        ),

                        "thunder" => array (
                                       35 => "Durant",
                                       0  => "Westbrook",
                                       13 => "Miller"
                        )
                    );

我想要显示如下内容:

球队名称:湖人

  1. Bryant = 24
  2. 价格= 6
  3. 林= 17
  4. 球队名称:尼克斯

    1. Anthony = 7
    2. Shumpert = 22
    3. 杰克逊= 12
    4. ...

      这是我尝试但似乎不起作用的代码:

      foreach ($team_arrays as $names => $team) {
                              echo "<h2>Team Name: " . $names . "</h2>";
                              echo "<ol>";
                                  foreach ($team_arrays as $jersey => $names) {
                                      echo "<li>" . $names . " = " . $jersey . "</li>";
                                  }
                              echo "</ol>";
                          }
      

      它会产生这种错误

      Notice: Array to string conversion in
      

      有人会尝试寻求帮助。请。

      我从其他问题中找到了这个solution,但似乎我无法与之相关。

3 个答案:

答案 0 :(得分:0)

您的代码中只有一个小问题

foreach ($team_arrays as $jersey => $names) {
         ^ // again looping over the same outer array?

内部循环错误,你应该遍历$team而不是$team_arrays,因为你的外部循环使用$team变量来接收每个团队。

foreach ($team as $jersey => $names) {

其余的代码和逻辑已经很好了。

<强> Fiddle

答案 1 :(得分:0)

您需要更改此行;

 foreach ($team_arrays as $jersey => $names) {

为:

   foreach ($team as $jersey => $names) {

答案 2 :(得分:0)

更改为foreach ($team as $jersey => $names)

试试这个:     

$team_arrays = array (
                        "lakers" => array (
                                        24 => "Bryant",
                                        6  => "Price",
                                        17 => "Lin"
                        ),

                        "knicks" => array (
                                        7 => "Anthony",
                                       22 => "Shumpert",
                                       12 => "Jackson"
                        ),

                        "thunder" => array (
                                       35 => "Durant",
                                       0  => "Westbrook",
                                       13 => "Miller"
                        )
                    );

 foreach ($team_arrays as $names => $team) {
                        echo "<h2>Team Name: " . $names . "</h2>";
                        echo "<ol>";
                            foreach ($team as $jersey => $names) {
                                echo "<li>" . $names . " = " . $jersey . "</li> \n";
                            }
                        echo "</ol>";
                    }                   
 ?>