按键对值进行分组,Foreach 循环内的多维数组 - PHP

Grouping values by Key, Multidimensional Array inside a Foreach Loop - PHP

我正在从 MySQL 中提取数据到一个多维数组中,我们称它为 $notes。 var_dump($notes) 给出类似的东西:

array (size=4)
  0 => 
    array (size=5)
      'id' => string '5' (length=1)
      'date' => string '2018-04-17 18:27:26' (length=19)
      'heading' => string 'Upcoming Event' (length=14)
      'name' => string 'The trip to Middleton has been cancelled' (length=40)
      'due_date' => string '2018-04-24' (length=10)
  1 => 
    array (size=5)
      'id' => string '7' (length=1)
      'date' => string '2018-04-17 18:30:49' (length=19)
      'heading' => string 'Special Offer' (length=13)
      'name' => string 'New Books in the Library. Get 20% discount if you buy before 29th April' (length=71)
      'due_date' => string '2018-04-29' (length=10)
  2 => 
    array (size=5)
      'id' => string '8' (length=1)
      'date' => string '2018-04-17 18:32:00' (length=19)
      'heading' => string 'Upcoming Event' (length=14)
      'name' => string 'There will be a PTA Meeting on 28th April 2018.' (length=47)
      'due_date' => string '2018-04-29' (length=10)
  3 => 
    array (size=5)
      'id' => string '3' (length=1)
      'date' => string '2018-04-17 16:43:44' (length=19)
      'heading' => string 'Warning' (length=7)
      'name' => string 'Please avoid illegal parking as that puts our members in danger.' (length=64)
      'due_date' => string '2018-07-24' (length=10);

我有一个 Foeach 循环来处理数组。这是我代码的相关部分。

<?php
       foreach ($notes as $row) {
           echo '<h3>'.$row[heading].'</h3>';
           echo '<h3>'.$row[name].'</h3>';
        }
?>

我得到类似的东西:

Upcoming Event

The trip to Middleton has been cancelled

Special Offer

New Books in the Library. Get 20% discount if you buy before 29th April

Upcoming Event

There will be a PTA Meeting on 28th April 2018.

Warning

Please avoid illegal parking as that puts our members in danger.

如您所见,有些标题可能出现不止一次,在这种情况下,标题下有2个项目,'Upcoming Events'。

问题: 如何将具有相同标题的项目组合在一起?在这种情况下,只有一个即将发生的事件标题,并且在该标题下有两个即将发生的事件?大约有 8 个标题可以使用。因此,如果每个项目都有一个独特的标题,则应单独显示这些项目。例如,如果提交的所有注释都在 Upcoming Events 下,那么 Heading Upcoming Event 应该出现一次,在其下列出所有即将发生的事件。

我认为除了 array_map 之外还需要一些自定义函数,但我没有想出这方面的任何东西。请帮忙。

如果您按标题对 array/results 进行排序,您应该能够只观察标题何时更改,如下所示:

<?php
       $lastheading = 'some impossible string';
       foreach ($notes as $row) {
           if ($row[heading] != $lastheading)
              echo '<h3>'.$row[heading].'</h3>';
           $lastheading = $row[heading];
           echo '<h3>'.$row[name].'</h3>';
        }
?>

您首先要做的是整理数据。您希望首先对数据进行分组。你通过排序来做到这一点。您可以创建自己的排序函数,然后使用 usort 函数。 之后你将不得不检查 lastheading 并打印,如果它不一样。

$arr = Array(0=>Array("id"=>"1",
                      "heading"=>"attack",
                      "name"=>"fire place"),
             1=>Array("id"=>"2",
                      "heading"=>"dog",
                      "name"=>"second place"),
             2=>Array("id"=>"3",
                      "heading"=>"attack",
                      "name"=>"he who climbs mountains")
    );
usort($arr,function($a,$b) {
    return strcmp($a["heading"], $b["heading"]);
});
$lastHeading = "";
foreach ($arr as $row) {
    if($lastHeading != $row["heading"])
        echo '<h3>'.$row["heading"].'</h3>';
    echo '<h5>'.$row["name"].'</h3>';
    $lastHeading = $row["heading"];
}