0

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.

0

2 Answers 2

1

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>';
        }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I sorted my Query results and your answer worked more than perfectly.
1

What you have to do first is organise you data. You want to group your data first. You do that by sorting. You can make you own sorting function and then use the usort function. After that you will have to check the lastheading and print if it's not the same.

$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"];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.