为新级别的数组添加额外级别的foreach循环

时间:2018-06-13 14:24:07

标签: php arrays foreach

我有一个额外的级别添加到一个多维数组,我正在尝试找到合并一个新的foreach循环或更改当前循环的最佳方法,以便我的模态正文为每个页面正确添加一个新的部分类型。

基本上,我的模式应该有一个标题用于每个公司和显示(房间),并且正文应该有一个部分用于该显示/房间中的每种页面类型。

到目前为止我对模态标题有好处,但现在我已经评论了一个新的部分,我需要一个新的循环来实际显示页面类型和过期。

这是迄今为止的代码:

<?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"),
      ),
    ),
  ),
),
);

?>

<div class="col-lg-4 col-md-12 col-sm-12">
<div class="page-header">
  <h2>Pages Expiring Soon</h2>
</div>
<div class="card" id="expDisplays" style="margin-bottom:20px; padding:10px;">
  <?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>

          <!--Everything works to this point, I need a new foreach to show page types and expirations-->
          <div class="modal-body">
            <!--Need new foreach to get page types and their corresponding expiration-->
            <br>
            <h6><?php echo $v['Type'] ?></h6>
            <h6>Date/Time Expiring: <?php echo $v["Expiration"] ?></h6>
            <h6>Set New Expiration: <input data-date-format="mm/dd/yyyy" id="datepicker" name="datepicker"></h6>
            <a href="#" style="float:left;">View Display</a>
            <br>
            <hr>
            <?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; ?>
  <?php endforeach; ?>
</div>

1 个答案:

答案 0 :(得分:1)

你需要在模态体内放置一个新的foreach循环。试试这个:

<div class="modal-body">
           <?php foreach($v as $k=>$n){ ?>
            <!--Need new foreach to get page types and their corresponding expiration-->
            <br>
            <h6><?php  echo $n['Type']; ?></h6>
            <h6>Date/Time Expiring: <?php echo $n["Expiration"] ?></h6>
            <h6>Set New Expiration: <input data-date-format="mm/dd/yyyy" id="datepicker" name="datepicker"></h6>
            <a href="#" style="float:left;">View Display</a>
            <br>
            <hr>
              <?php } endforeach; ?>
            <!-- END MODAL BODY -->
          </div>