I have two arrays. The first comes directly from my original data. Each item is a point for a timeline.
print_r($items)
returns
Array
(
[0] => Array
(
[id] => 1173
[month] => January
[year] => 2013
[description] => This is a test
[link] => #
[image] => http://s3.amazonaws.com/stockpr-test-store/esph2/db/Timeline/1173/image_resized.jpg
)
[1] => Array
(
[id] => 1183
[month] => February
[year] => 2013
[description] => This is another test
[link] =>
)
[2] => Array
(
[id] => 1193
[month] => December
[year] => 2012
[description] => Testing another year
[link] => #
)
)
I have a second array that gets all the unique years from that array.
print_r($years)
returns
Array
(
[0] => 2013
[2] => 2012
)
How can I then go through the years and then per year return the items that match that year?
So, for 2012, I would get [2] from the $items array. For 2013, I would get [0] and [1] from the $items array.
Am I going at this all wrong? All I want to do is have an output like this:
<h1>Timeline</h1>
<h2>2013</h2>
<ul>
<li>ID No. 1173</li>
<li>ID No. 1183</li>
</ul>
<h2>2012</h2>
<ul>
<li>ID No. 1193</li>
</ul>
Edit:
Currently using:
<? foreach($years as $year) : ?>
<div class="year row clearfix" id="y-$year">
<h3><span><?= $year ?></span></h3>
<? foreach($items as $item) : ?>
<? if($item['year'] == $year) : ?>
<div class="item span5">
<div class="padding">
<div class="text">
<h4><?= $item['month']?> <?= $item['year'] ?></h4>
<p><?= $item['description'] ?></p>
</div>
</div>
</div>
<? endif; ?>
<? endforeach; ?>
</div>
<? endforeach; ?>
But it seems a little inefficient. From what I can understand, it's going through the full array every time it tries to output an item.
array_filter()
should work. – Pitchinnate Feb 15 '13 at 21:50