使用foreach循环从此数组生成<select>的最佳方法是什么

时间:2018-08-20 22:01:36

标签: php multidimensional-array foreach

我有以下多维数组: 数组   ( [0] =>数组     (         [ID] => 6         [名称] =>主要运动         [值] =>数组             (                 [ClientIndexValue] =>数组                     (                         [0] =>数组                             (                                 [ID] => 23                                 [名称] =>拳击                                 [有效] => 1                             )                         [1] =>数组                             (                                 [ID] => 24                                 [名称] =>骑自行车                                 [有效] => 1                             )                         [2] =>数组                             (                                 [ID] => 25                                 [名称] =>运行                                 [有效] => 1                             )                     )             )     ) [1] =>数组     (         [ID] => 46         [名称] =>主要健身目标         [值] =>数组             (                 [ClientIndexValue] =>数组                     (                         [0] =>数组                             (                                 [ID] => 512                                 [名称] =>比赛                                 [有效] => 1                             )                         [1] =>数组                             (                                 [ID] => 513                                 [名称] =>灵活性                                 [有效] => 1                             )                         [2] =>数组                             (                                 [ID] => 514                                 [名称] =>开心                                 [有效] => 1                             )                     )             )     ) [2] =>数组     (         [ID] => 61         [名称] =>军事         [值] =>数组             (                 [ClientIndexValue] =>数组                     (                         [0] =>数组                             (                                 [ID] => 550                                 [名称] =>海军陆战队                                 [有效] => 1                             )                         [1] =>数组                             (                                 [ID] => 551                                 [名称] =>空军                                 [有效] => 1                             )                     )             )     ) ) 我想做的是使用PHP为每个数组创建一个 我知道在这里可以选择使用foreach循环,但是我很难准确地了解如何为数组的多个维度构造foreach循环。我发现的所有示例的结构如下: foreach($ first_array as $ key => $ value){   foreach($ second_array as $ second){       如果($ second ['menu_url'] == $ key){           回声“嗨”;        }       }      } 如何访问第三个阵列?

2 个答案:

答案 0 :(得分:1)

问题中的代码距离不太远。

foreach ($array as $entry) {
  echo $entry['Name'];

  foreach ($entry['Values']['ClientIndexValue'] as $option) {
    echo $option['Name'];
  }
}

这就是你所追求的。

答案 1 :(得分:0)

学习是对模式的检测,大多数教程在可视化模式方面都非常糟糕。我总是使用$k1 => $v1并增加变量的整数。如果您有第四层的数组,只需按foreach ($v2 as $k3 => $v3)上的Enter键并复制其上方的内容即可。

<?php
foreach ($array_first as $k1 => $v1)
{
 if (is_array($v1))
 {
  foreach ($v1 as $k2 => $v2)
  {
   if (is_array($v2))
   {
    foreach ($v2 as $k3 => $v3)
    {
     //Presuming no fourth level array:
     echo '<option>---Third Level</option>';
    }
   }
   else {echo '<option>--Second Level</option>';}//not array
  }
 }
 else {echo '<option>-First Level</option>';}//not array
}
?>

在要查找特定数据的其他情况下,请不要忘记使用break