Foreach:对相同的数据进行分组



我正在尝试按日期对foreach语句中生成的数据进行分组。我正在使用柯比 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

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

已编辑 - 如果您希望日期之间的中断,您可以在if内关闭/打开<li>/</li>

  <?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]);
}

相关内容

  • 没有找到相关文章

最新更新