循环多维数组,只获取第一个元素

时间:2018-06-18 12:24:22

标签: php foreach

我遇到了我正在测试的硬编码数组的问题。我正在循环一些html和php来为每个顶级项目创建一个标题列表,这是有效的。 但是我再次循环为每个项目创建一个模态,它只对应于第一个项目。

这是数组,因为它是硬编码的:

  <?php 

  $displays = array(
    "Company" => array(
      "Company One Main"=>array(
        "Displays"=>array(
          "Room One"=>array(
            "Page"=>array(
              "Type"=>"News",
              "Expiration"=>"06/12/2018"),
          ),
          "Room Two"=>array(
            "Page"=>array(
              "Type"=>"Social",
              "Expiration"=>"06/18/2018"),
          ),
          "Room Three"=>array(
            "Page"=>array(
              "Type"=>"Social",
              "Expiration"=>"06/18/2018"),
          ),
        ),
      ),
      "Company One Other"=>array(
        "Displays"=>array(
          "Room Two"=>array(
            "Page"=>array(
              "Type"=>"Social",
              "Expiration"=>"06/18/2018"),
          ),
          "Room Three"=>array(
            "Page"=>array(
              "Type"=>"Social",
              "Expiration"=>"06/18/2018"),
          ),
        ),
      ),
      "Company Two Main"=>array(
        "Displays"=>array(
          "Room One"=>array(
            "Page"=>array(
              "Type"=>"Social",
              "Expiration"=>"06/18/2018"),
          ),
          "Room Two"=>array(
            "Page"=>array(
              "Type"=>"Social",
              "Expiration"=>"06/18/2018"),
          ),
        ),
      ),
      "Company Two Other"=>array(
        "Displays"=>array(
          "Room One"=>array(
            "Page"=>array(
              "Type"=>"Social",
              "Expiration"=>"06/18/2018"),
          ),
          "Room Two"=>array(
            "Page"=>array(
              "Type"=>"Social",
              "Expiration"=>"06/18/2018"),
          ),
          "Room Three"=>array(
            "Page"=>array(
              "Type"=>"Social",
              "Expiration"=>"06/18/2018"),
          ),
        ),
      ),
    ),
  );

  ?>

所以我的第一段代码可以通过创建4个区域的列表来正常工作,如下所示:

问题是,当我单击四个公司中的任何一个的编辑时,它显示的模式仅包含数组中第一个元素的信息,即Company One Main。 布局是正确的,但它只显示第一家公司的房间,页面类型和到期日,而不是每个列表项都有一个模式,用于它自己的相应信息。

所以无论我点击哪一个,我都会看到:

我可能只是错误地循环。这是PHP和HTML

  /*These first two lines are correct, they created the list of areas with 'edit' links*/

  <?php foreach($displays["Company"] as $area_name => $area_details): ?>
    <h6><?php echo $area_name ?><a href="#" data-toggle="modal" data-target="#expiringDisplays">Edit</a></h6>
      <!-- Modal -->
      <div class="modal fade" id="expiringDisplays" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
            <div class="modal-header">
            <?php foreach($area_details["Displays"] as $d=>$v): ?>
            <h5 class="modal-title" id="modalLabel"><?php echo  $area_name . " - " . $d?></h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
            <!-- END MODAL HEADER -->
            </div>
            <div class="modal-body">
              <?php foreach($v as $k=>$n): ?>
              <!--Body of the Modal for expired displays-->
              <br>
              <h6><?php echo $n['Type'] ?></h6>
              <h6>Date/Time Expiring: <?php echo $n["Expiration"] ?></h6>
              <h6>Set New Expiration:</h6>                                     
              <a href="#" style="float:left;">View Display</a>
              <br>
              <hr>
              <?php endforeach; ?>
              <?php endforeach; ?>
              <!-- END MODAL BODY -->
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
              <button type="button" class="btn btn-primary">Save changes</button>
              <!-- END MODAL FOOTER -->
            </div>
            <!-- END MODAL CONTENT -->
          </div>
          <!-- END MODAL DIALOGUE -->
      </div>
      <!-- END MODAL -->
      </div>
  <?php endforeach; ?>

2 个答案:

答案 0 :(得分:1)

您为所有编辑链接提供了相同的锚链接。默认情况下,它将选择具有此id的第一个元素。您需要为每个模态提供自己的ID。

如果您确定&#34;公司名称&#34;是唯一的你可以使用它们作为id(之前替换一些字符),但你可以使用数字id后缀(我认为会更好):

<?php
$n_id = 0;
foreach($displays["Company"] as $area_name => $area_details) {
    ?>
    <h6><?php echo $area_name ?><a href="#" data-toggle="modal" data-target="#modal_<?php echo $n_id; ?>">Edit</a></h6>
    <!-- Modal -->
    <div class="modal fade" id="modal_<?php echo $n_id; ?>" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
    <!-- The rest of your code ... -->
    </div>
    <?php
    $n_id += 1;
}
?>

答案 1 :(得分:0)

尝试这样的事情

$i = 0 

foreach ($data as $k => $v) {
  echo '<a href="#" data-toggle="modal" data-target="#expiringDisplays_'.$i.'">Edit</a>';
// and your give the same to div tag like this
echo '<div class="modal fade" id="expiringDisplays_'.$i.'" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true"></div>';
$i++;
}

希望你明白我的观点