Search code examples
phpmysqlarraysforeacharray-map

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


I am pulling data from MySQL into a Multidimensional Array, let's call it $notes. var_dump($notes) gives something like:

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);

I have a Foeach Loop to process the Array. Here the relevant portion of my code.

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

I get something like:

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.

As you can see, Some headings may appear more than once, in this case, there are 2 items under the heading, 'Upcoming Events'.

Question: How do I group items with the same Heading together? In this case, have only one Upcoming Event Heading and have the two upcoming events under that Heading? There are about 8 Headings that can be used. So if each item, has a unique heading, the items should be displayed separately. If for instance, all Notes submitted are under Upcoming Events, then the Heading Upcoming Event should appear once, under which all the upcoming events are listed.

I think it needs some custom Function alongside array_map, but I have failed to come up with anything in that direction. Please help.


Solution

  • If you sort your array/results by heading, you should be able to just watch for when the heading changes, like so:

    <?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>';
            }
    ?>