Foreach:分组相同的数据

Foreach: Group same data

我正在尝试按日期对从 foreach 语句生成的数据进行分组。我正在使用 Kirby CMS,但在完成以下操作时遇到了麻烦。

目标(sudo):

date: 03/10/15
Take Over

date: 03/09/15
Video
Flash
Super GIF 2
Super GIF
Image

我的尝试:

date: 03/10/15
Take Over
date: 03/09/15
Video
date: 03/09/15
Flash
date: 03/09/15
Super GIF 2
date: 03/09/15
Super GIF
date: 03/09/15
Image

代码:

  <ul>
    <?php foreach($page->children()->visible()->sortBy('modified', 'desc')  as $projects): ?>
      <li>
        date: <?php echo $projects->modified('m/d/y') ?> <br>
        <a href="<?php echo $projects->url() ?>"><?php echo $projects->title()->html() ?></a>
      </li>
    <?php endforeach  ?>  
  </ul>

第二次失败尝试:

  <ul>
      <?php foreach($page->children()->visible()->sortBy('modified', 'desc')  as $projects): ?>
       <li>
          <?php foreach($page->children()->visible()->sortBy('modified', 'desc')  as $project): ?>

            <?php 
              if($projects->modified('m/d/y') == $project->modified('m/d/y')) {
                echo $project->modified('m/d/y') . '<br>' . $project->title();
              }

            ?>

          <?php endforeach  ?>  
       </li>


    <?php endforeach  ?>  
  </ul>

输出:

03/10/15
Take Over
03/09/15
Video 03/09/15
Flash 03/09/15
Super GIF 2 03/09/15
Super GIF 03/09/15
Image
03/09/15
Video 03/09/15
Flash 03/09/15
Super GIF 2 03/09/15
Super GIF 03/09/15
Image
03/09/15
Video 03/09/15
Flash 03/09/15
Super GIF 2 03/09/15
Super GIF 03/09/15
Image
03/09/15
Video 03/09/15
Flash 03/09/15
Super GIF 2 03/09/15
Super GIF 03/09/15
Image
03/09/15
Video 03/09/15
Flash 03/09/15
Super GIF 2 03/09/15
Super GIF 03/09/15
Image

创建一个变量来保存当前日期 - $currentDate,然后检查 $projects->modified('m/d/y') 是否与 $currentDate 匹配,如果不匹配,将其设置为 $currentDate 和回声-

  <?php $currentDate = ''; ?>
  <ul>
    <?php foreach($page->children()->visible()->sortBy('modified', 'desc')  as $projects): ?>
      <li>
        <?php if($projects->modified('m/d/y') != $currentDate) { $currentDate = $projects->modified('m/d/y'); ?>
        date: <?php echo $projects->modified('m/d/y') ?> <br>
        <?php } ?>
        <a href="<?php echo $projects->url() ?>"><?php echo $projects->title()->html() ?></a>
      </li>
    <?php endforeach  ?>  

edited - 如果你想要日期之间的休息时间,你可以 close/open <li>/</li> inside if

  <?php $currentDate = ''; ?>
  <ul>
    <?php foreach($page->children()->visible()->sortBy('modified', 'desc')  as $projects): ?>
      <li>

        <?php if($projects->modified('m/d/y') != $currentDate) { 
            if($currentDate != '') { echo "</li><li>"; } // if not the 1st date, add a blank space/li before the next date 
            $currentDate = $projects->modified('m/d/y'); ?>
        date: <?php echo $projects->modified('m/d/y') ?>
        <?php } ?>

        <br><a href="<?php echo $projects->url() ?>"><?php echo $projects->title()->html() ?></a>

    <?php endforeach  ?>  
      </li>
  </ul>

基本上您可能想要做的是将您的列表重新组织到容器中。

** 注意:全部未经测试,但应该接近 **

这可以像这样简单:

$containers = [];
foreach ($bar as $foo) {
  $containers[$foo['date']][] = $foo
}

ksort($containers);

如果您使用的是 PHP5.5+,那么您的运气就更好了:

$containers = array_column($bar, null, 'date');
ksort($containers);

这应该为您提供循环所需的结构:

$dates = array_keys($containers);
foreach ($dates as $container) {
  echo 'date: '.$container;
  var_dump($containers[$container]);
}