I have an array that has some html in it as values. In that html are dates. I'd like to sort the array by those dates.
Array looks like:
Array(
[0] => '<div class="date">April 6, 2016</div>'
[1] => '<div class="date">January 23, 2016</div>'
[2] => '<div class="date">March 6, 2016</div>'
[3] => '<div class="date">December 5, 2016</div>'
)
What I'd like to get is:
Array(
[0] => '<div class="date">January 23, 2016</div>'
[1] => '<div class="date">March 6, 2016</div>'
[2] => '<div class="date">April 6, 2016</div>'
[3] => '<div class="date">December 5, 2016</div>'
)
Just using sort($array, SORT_NUMERIC)
or rsort()
, doesn't work. My guess is that I need to use usort()
and then create a function that will compare stripped array values (only dates). But I don't know how to start.
Any help is welcome.
EDIT
I started working on a function:
function strip_sort_array($a){
foreach ($a as $key => $value) {
$date = substr($value, 18, 32);
}
}
This returns the date. This can be converted to unix with strtotime. I just need to use this for comparison somehow...
strtotime()
. You already mentioned that you know aboutusort()
. So use it. If you have specific problems doing that, ask for more specific help. – TecBrat Jan 5 '16 at 14:36