Jump to content

[SOLVED] Sort array chronologically


soycharliente

Recommended Posts

I'm trying to sort an array chronologically. After a bit of searching, the best way I can find it to use uasort() with a callback function.

 

Can someone help me understand this example and how I could transform it to compare dates? The dates are in a multidimensional array.

$feed[0]['release_date']

 

<?php
// Comparison function
function cmp($a, $b) {
    if ($a == $b) return 0;
    return ($a < $b) ? -1 : 1;
}

// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
print_r($array);

// Sort and print the resulting array
uasort($array, 'cmp');
print_r($array);
?>

Link to comment
Share on other sites

What is the format of the date? That would determine how you compare the dates, but assuming the dates are in the format yyyymmdd, then you could do this:

 

function sortByreleaseDate($a, $b)
{
    if ($a['release_date'] = $b['release_date']) { return 0; }
    return ($a['release_date'] < $b['release_date']) ? -1 : 1;
}

uasort($feed, 'sortByReleaseDate');

Link to comment
Share on other sites

It's stored as 2009-07-24 14:16:00 (I can easily concatenate the date and time together). And it's another dimension down in the array. Does that throw a wrench in the mix?

 

Array
(
    [0] => Array
        (
            [release_date] => 2009-07-24
            [release_time] => 14:16:00
            [display_start_date] => 2009-07-24
            [display_stop_date] => 2015-12-31
            [id] => 3179
            [title] => Event Title
            [summary] => A one sentence summary about the event.
            [img_embed] => 
            [img_alt] => 
        )

    [1] => Array
        (
            [release_date] => 2009-07-23
            [release_time] => 11:07:00
            [display_start_date] => 2009-07-23
            [display_stop_date] => 2009-09-01
            [id] => 3172
            [title] => Event Title
            [summary] => A one sentence summary about the event.
            [img_embed] => 
            [img_alt] => 
        )

    [2] => Array
        (
            [release_date] => 2009-07-09
            [release_time] => 11:55:00
            [display_start_date] => 2009-07-09
            [display_stop_date] => 2009-11-09
            [id] => 3130
            [title] => Event Title
            [summary] => A one sentence summary about the event.
            [img_embed] => 
            [img_alt] => 
        )
)

Link to comment
Share on other sites

Buddy told me that $a and $b get passed associative arrays. Got it working now.

 

Final solution:

function cmp($a, $b) {
  if($a['display_start_date'] == $b['display_start_date']) { return 0; }
  return ($a['display_start_date'] > $b['display_start_date']) ? -1 : 1;
}
usort($feed, 'cmp');

 

Pretty close to what you had mjdamato (if not the exact same thing--it looks the same). I was messing with yours and showed them what you gave me. What I posted is what we eventually finished with.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.