I have the following $MessagesArray
PHP bidimensional indexed array of one-dimensional associative inner arrays:
Array
(
[0] => Array
(
[id] => 1
[from] => Person 1
[to] => Person 2
[platform] => Instagram Direct Messaging
[date] => 2016/11/27
[time] => 11:00
[ampm] => AM
[specialcontent] => none
[content] => Hello
)
[1] => Array
(
[id] => 2
[from] => Person 1
[to] => Person 2
[platform] => Instagram Direct Messaging
[date] => 2016/11/27
[time] => 11:00
[ampm] => AM
[specialcontent] => none
[content] => How are you?
)
[2] => Array
(
[id] => 3
[from] => Person 2
[to] => Person 1
[platform] => Instagram Direct Messaging
[date] => 2016/11/27
[time] => 6:00
[ampm] => PM
[specialcontent] => none
[content] => Oh, hey there. I'm fine
)
[3] => Array
(
[id] => 4
[from] => Person 2
[to] => Person 1
[platform] => Instagram Direct Messaging
[date] => 2016/11/27
[time] => 6:01
[ampm] => PM
[specialcontent] => none
[content] => What about you?
)
[4] => Array
(
[id] => 5
[from] => Person 1
[to] => Person 2
[platform] => Instagram Direct Messaging
[date] => 2016/11/28
[time] => 8:00
[ampm] => AM
[specialcontent] => none
[content] => It's been a while.
)
[5] => Array
(
[id] => 6
[from] => Person 1
[to] => Person 2
[platform] => Instagram Direct Messaging
[date] => 2016/11/30
[time] => 2:00
[ampm] => PM
[specialcontent] => none
[content] => Hello?
)
[6] => Array
(
[id] => 7
[from] => Person 2
[to] => Person 1
[platform] => Instagram Direct Messaging
[date] => 2016/12/01
[time] => 3:00
[ampm] => PM
[specialcontent] => none
[content] => Sorry, I'm traveling 'till next year.
)
[7] => Array
(
[id] => 8
[from] => Person 1
[to] => Person 2
[platform] => Instagram Direct Messaging
[date] => 2016/12/05
[time] => 1:00
[ampm] => PM
[specialcontent] => none
[content] => Fine by me.
)
[8] => Array
(
[id] => 9
[from] => Person 2
[to] => Person 1
[platform] => Instagram Direct Messaging
[date] => 2017/01/04
[time] => 3:00
[ampm] => PM
[specialcontent] => none
[content] => I'm back.
)
[9] => Array
(
[id] => 10
[from] => Person 1
[to] => Person 2
[platform] => Instagram Direct Messaging
[date] => 2018/01/15
[time] => 1:00
[ampm] => PM
[specialcontent] => none
[content] => I'm back too, one year later too.
)
)
Whose actual code is (although I actually created the previous array from a database in a XML file):
<?php
$MessagesArray = array(
array(
"id" => "1",
"from" => "Person 1",
"to" => "Person 2",
"platform" => "Instagram Direct Messaging",
"date" => "2016/11/27",
"time" => "12:00",
"ampm" => "AM",
"specialcontent" => "none",
"content" => "Hello"
),
array(
"id" => "2",
"from" => "Person 1",
"to" => "Person 2",
"platform" => "Instagram Direct Messaging",
"date" => "2016/11/27",
"time" => "11:00",
"ampm" => "AM",
"specialcontent" => "none",
"content" => "How are you?"
),
array(
"id" => "3",
"from" => "Person 2",
"to" => "Person 1",
"platform" => "Instagram Direct Messaging",
"date" => "2016/11/27",
"time" => "6:00",
"ampm" => "PM",
"specialcontent" => "none",
"content" => "Oh, hey there. I'm fine"
),
array(
"id" => "4",
"from" => "Person 2",
"to" => "Person 1",
"platform" => "Instagram Direct Messaging",
"date" => "2016/11/27",
"time" => "6:01",
"ampm" => "PM",
"specialcontent" => "none",
"content" => "What about you?"
),
array(
"id" => "5",
"from" => "Person 1",
"to" => "Person 2",
"platform" => "Instagram Direct Messaging",
"date" => "2016/11/28",
"time" => "8:00",
"ampm" => "AM",
"specialcontent" => "none",
"content" => "It's been a while."
),
array(
"id" => "6",
"from" => "Person 1",
"to" => "Person 2",
"platform" => "Instagram Direct Messaging",
"date" => "2016/11/30",
"time" => "2:00",
"ampm" => "PM",
"specialcontent" => "none",
"content" => "Hello?"
),
array(
"id" => "7",
"from" => "Person 2",
"to" => "Person 1",
"platform" => "Instagram Direct Messaging",
"date" => "2016/12/01",
"time" => "3:00",
"ampm" => "PM",
"specialcontent" => "none",
"content" => "Sorry, I'm traveling 'till next year."
),
array(
"id" => "8",
"from" => "Person 1",
"to" => "Person 2",
"platform" => "Instagram Direct Messaging",
"date" => "2016/12/05",
"time" => "1:00",
"ampm" => "PM",
"specialcontent" => "none",
"content" => "Fine by me."
),
array(
"id" => "9",
"from" => "Person 2",
"to" => "Person 1",
"platform" => "Instagram Direct Messaging",
"date" => "2017/01/04",
"time" => "3:00",
"ampm" => "PM",
"specialcontent" => "none",
"content" => "I'm back."
),
array(
"id" => "10",
"from" => "Person 1",
"to" => "Person 2",
"platform" => "Instagram Direct Messaging",
"date" => "2018/01/15",
"time" => "1:00",
"ampm" => "PM",
"specialcontent" => "none",
"content" => "I'm back too, one year later too."
)
);
?>
And I'd like to somehow have a code that automatically creates HTML, with the following example:
<div class="year">
2016
<div class="month">
Month 11
<div class="day">
Day 27
</div>
<div class="day">
Day 28
</div>
<div class="day">
Day 30
</div>
<div class="month">
Month 12
<div class="day">
Day 01
</div>
<div class="day">
Day 05
</div>
</div>
</div>
</div>
<div class="year">
2017
<div class="month">
Month 01
<div class="day">
Day 04
</div>
</div>
</div>
<div class="year">
2018
<div class="month">
Month 01
<div class="day">
Day 15
</div>
</div>
</div>
As you can see, it isn't really that hard, I'm just having a problem figuring out how to use nested foreach
functions.
The code should produce a <div>
HTML element for each unique year, then its corresponding <div>
HTML element for each unique month of that year, and then at the same time its corresponding <div>
HTML element for each day month of that month of that year.
I think we need to use nested foreach
functions. Suppose we have the code foreach ($MessagesArray as $item) {}
, then, inside the function, we could get the year, month, and day as $year = substr($item['date'], 0, 4);
, $month = substr($item['date'], 5, 2);
, and $day = substr($item['date'], 8, 2);
, respectively. I also think we need to someone know the value of the previous iteration.