Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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)
share|improve this question
3  
Is this coming from a database? If not, why not? – NullUserException Apr 5 '12 at 19:26
from where is this array being loaded? – hjpotter92 Apr 5 '12 at 19:27
3  
Yep, looks suspiciously like MySQL query rows. ORDER BY created DESC – Michael Berkowski Apr 5 '12 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? – Daniel West Apr 5 '12 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 – Arjan Apr 5 '12 at 19:43

4 Answers

up vote 2 down vote accepted

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);
?>
share|improve this answer
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. – Arjan Apr 5 '12 at 19:41
thanks, answer updated (i left out the equal case, i know..) – stewe Apr 5 '12 at 19:45

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

share|improve this answer
2  
And how's that going to help sort the 2D array? – Michael Berkowski Apr 5 '12 at 19:28
Heh ok, vague question. I thought OP was implying his stamps were Y-d-m. – Alex Howansky Apr 5 '12 at 19:31
1  
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. – Arjan Apr 5 '12 at 19:33

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;
}
share|improve this answer

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

share|improve this answer
1  
This will produce an ascending order rather than descending. – MrCode Apr 5 '12 at 19:44
1  
I updated my answer. Fortunately it's only a small change. – Arjan Apr 5 '12 at 19:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.