Reputation: 33
My data comes from a database, and has been grouped by
day_id
, so the array is sorted by day_id
.
Code + sample output provided, kind of hard to put in words.
I got my inspiration so far from this accepted answer, but I need a slight adjustment.
$array = array(
0 => array(
'id' => '5',
'day_id' => '1',
'Foo' => array(
'id' => '28',
'name' => 'My day_id is 1'
)
),
1 => array(
'id' => '6',
'day_id' => '1',
'Foo' => array(
'id' => '29',
'name' => 'My day_id is 1'
)
),
2 => array(
'id' => '7',
'day_id' => '2',
'Foo' => array(
'id' => '30',
'name' => 'My day_id is 2'
)
),
3 => array(
'id' => '8',
'day_id' => '3',
'Foo' => array(
'id' => '31',
'name' => 'My day_is 3'
)
)
);
And my code to output it:
$day = 0;
foreach($array as $a) {
// $a['day_id'] = 1, condition is true. output header once.
if($day != $a['day_id']) {
echo '<h3>The Day is ' . $a['day_id'] . '</h3>' . "\n";
}
// output the elements Foo key content.
echo $a['Foo']['name'] . "\n";
// store the current value to compare on the next iteration
// $a['day_id'] will eventually be 2, causing the condition to fire again.
$day = $a['day_id'];
}
This outputs:
<h3>The Day is 1</h3>
My day_id is 1
My day_id is 1
<h3>The Day is 2</h3>
My day_id is 2
<h3>The Day is 3</h3>
My day_is 3
I now want to wrap each "Day" in a DIV, but I cannot figure out the logic. If I replace the <h3>
with an open div, I do not know where to put the closing </div>
. My attempts thus far have just led to each day having an open
div, but not a closed one so they are nested, rather than separate.
I can't put the </div><div class="day-X">
as there will be an extra </div>
at the start which would break the layout.
Desired output:
<div class="day-1">
<h3>The Day is 1</h3>
My day_id is 1
My day_id is 1
</div>
<div class="day-2">
<h3>The Day is 2</h3>
My day_id is 2
</div>
<div class="day-3">
<h3>The Day is 3</h3>
My day_is 3
</div>
Hope this makes sense - thanks
Upvotes: 2
Views: 92
Reputation: 3018
You will need to loop twice, one for the days and one for the items that has this day. Without using any other SQL queries you will need to store the days in an array first. then you could loop the days and for each day you will echo the <div>
and the day header, at the end you will echo the </div>
.
inside the foreach you can loop again using the same array to check each item if it's has the same day or not. if yes then echo the name of the item, otherwise don't do anything.
I have tested this code, you can check it and let me know if you are having any problems with it.
foreach($array as $day){
$days[] = $day['day_id']; // store all the days in an array
}
// now days[] has [1,1,2,3]
$days = array_unique($days); // get unique days only no duplicated
//now days[] has [1,2,3]
foreach($days as $d){ // for each day
echo '<div class = "day-'.$d.'">';
echo '<h3>The Day is ' . $d . '</h3>' . "\n";
foreach($array as $a) { // check all other items if the item has the same day then echo
if($a['day_id'] == $d){
echo $a['Foo']['name'] . "\n";
}
}
echo '</div>';
}
Output:
<div class = "day-1">
<h3>The Day is 1</h3>
My day_id is 1
My day_id is 1
</div>
<div class = "day-2">
<h3>The Day is 2</h3>
My day_id is 2
</div>
<div class = "day-3">
<h3>The Day is 3</h3>
My day_is 3
</div>
Upvotes: 2