0

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

目标(须藤):

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
4

2 回答 2

1

创建一个变量来保存当前日期 - $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  ?>  

编辑- 如果你想要日期之间的休息,你可以关闭/打开<li>/</li>里面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>
于 2015-03-10T18:56:50.380 回答
1

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

** 注意:所有未经测试,但应该接近 **

这可以像这样简单:

$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]);
}
于 2015-03-10T19:00:12.077 回答