5

I currently have a problem within PHP where I want to sort these posts by their creation date so that they can then be shown in descending order. I have been looking for a PHP function to do this but have had no luck.

Is there an easy solution to this? Any idea will be greatly appreciated :)

array
      0 => 
        array
          'post_id' => string '1' (length=1)
          'user_id' => string '3' (length=1)
          'post' => string 'this is a post' (length=14)
          'created' => string '2012-04-05 20:11:38' (length=19)
     1 => 
        array
          'post_id' => string '2' (length=1)
          'user_id' => string '2' (length=1)
          'post' => string 'this is a post' (length=14)
          'created' => string '2012-04-05 20:11:38' (length=19)
     2 => 
        array
          'post_id' => string '3' (length=1)
          'user_id' => string '5' (length=1)
          'post' => string 'this is a post' (length=14)
          'created' => string '2012-04-05 20:11:38' (length=19)
5
  • 4
    Is this coming from a database? If not, why not? Commented Apr 5, 2012 at 19:26
  • from where is this array being loaded? Commented Apr 5, 2012 at 19:27
  • 3
    Yep, looks suspiciously like MySQL query rows. ORDER BY created DESC Commented Apr 5, 2012 at 19:27
  • It is coming from a database but because i have to grab the posts of each user seperately im trying to combine it... is there a better way of doing something like .... WHERE user_id = array(2,3,5) in MySQL? Commented Apr 5, 2012 at 19:37
  • 2
    That's hard to say if we can't see your query. WHERE user_id IN (2,3,5) is valid in MySQL Commented Apr 5, 2012 at 19:43

5 Answers 5

5

Try this:

<?php
$a=array(
      0 => 
        array(
          'post_id' => '1',
          'user_id' => '3',
          'post' => 'this is a post',
          'created' => '2012-04-05 20:11:40'
          ),
     1 => 
        array(
          'post_id' => '2',
          'user_id' => '2',
          'post' => 'this is a post',
          'created' => '2012-04-05 20:11:39'
          ),
     2 => 
        array(
          'post_id' => '3',
          'user_id' => '5',
          'post' => 'this is a post',
          'created' => '2012-04-05 20:11:38'
          )
);
function cmp($a,$b){
    return strtotime($a['created'])<strtotime($b['created'])?1:-1;
};

uasort($a,'cmp');
print_r($a);
?>
Sign up to request clarification or add additional context in comments.

2 Comments

cmp() should return a negative value if $a is considered smaller than $b, a positive value if $a is greater than $b and 0 if they are equal.
There is no need for strtotime. The format YYYY-MM-DD HH:MM:SS (24 hours) just needs the strcmp function for sorting.
2

Sorting array of records/assoc_arrays by specified mysql datetime field and by order:

    function build_sorter($key, $dir='ASC') {
        return function ($a, $b) use ($key, $dir) {
            $t1=strtotime(is_array($a)?$a[$key]:$a->$key);
            $t2=strtotime(is_array($b)?$b[$key]:$b->$key);
            if($t1==$t2) return 0;
            return (str_to_upper($dir)=='ASC'?($t1 < $t2):($t1 > $t2)) ? -1 : 1;
        };
    }


    // $sort - key or property name 
    // $dir - ASC/DESC sort order or empty
    usort($arr, build_sorter($sort, $dir));

Comments

1

You can use the usort() function which allows you to sort an array based on your own criteria.

function cmp($a, $b)
{
    if ($a['created'] == $b['created']) {
        return 0;
    }

    return ($a['created'] < $b['created']) ? 1 : -1;
}

usort($myArray, "cmp");

print_r($myArray);

Or if you want to convert to a time:

function cmp($a, $b)
{
    if ($a['created'] == $b['created']) {
        return 0;
    }

    $aInt = strtotime($a['created']);
    $bInt = strtotime($b['created']);

    return ($aInt < $bInt) ? 1 : -1;
}

Comments

1

You can sort an array using a custom sort function, like so:

function cmp($a, $b) {
    if($a['created'] < $b['created']) {
        return 1;
    } else if ($a['created'] > $b['created']) {
        return -1;
    } else  {
        // The only option left is that they are equal
        return 0;
    }
}

usort($array, cmp);

For more information about usort, check the php manpage

1 Comment

This will produce an ascending order rather than descending.
1

You can use strtotime() to convert the timestamp to an integer.

3 Comments

And how's that going to help sort the 2D array?
Heh ok, vague question. I thought OP was implying his stamps were Y-d-m.
If the dates are in Y-d-m format, you can't easily convert them to timestamps. And if they are in Y-m-d format, you don't need to convert them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.