Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Multidimensional Array Sort array_reverse not working?

I have a Multidimensional array that I am trying feverishly to properly sort by the date.

So far I've got:

usort($respArr, function($a, $b){
        $t1 = strtotime($a['PublishDate']);
        $t2 = strtotime($b['PublishDate']);
        return $t1 - $t2;
    });

array_reverse($respArr);
echo '<pre>';
print_r($respArr);
echo '</pre>';
echo '<hr />';

Here's some of the output from the above:

Array
(
[34] => Array
    (
        [Title] => o7thwd: Partnered with Internet Services Inc., to offer complete online presence! http://t.co/1SgAF66Tbf @ISI_Cloud
        [Link] => http://twitter.com/o7thwd/statuses/306088158574022656
        [PublishDate] => 2/25/2013 12:07:58 PM
        [Image] => 
        [Description] => o7thwd: Partnered with Internet Services Inc., to offer complete online presence! http://t.co/1SgAF66Tbf @ISI_Cloud
    )

[35] => Array
    (
        [Title] => o7thwd: Hangin with Mama Bear giving her the basics of social networking
        [Link] => http://twitter.com/o7thwd/statuses/307916797066240000
        [PublishDate] => 3/2/2013 1:14:19 PM
        [Image] => 
        [Description] => o7thwd: Hangin with Mama Bear giving her the basics of social networking
    )

[36] => Array
    (
        [Title] => o7thwd: o7th Web Design Articles http://t.co/plP8OkB7So
        [Link] => http://twitter.com/o7thwd/statuses/308687737400205313
        [PublishDate] => 3/4/2013 4:17:46 PM
        [Image] => 
        [Description] => o7thwd: o7th Web Design Articles http://t.co/plP8OkB7So
    )

[37] => Array
    (
        [Title] => o7thwd: finished up my GUI for a YUI minifier: http://t.co/7DKLoCXJHw
        [Link] => http://twitter.com/o7thwd/statuses/309309871931682816
        [PublishDate] => 3/6/2013 9:29:54 AM
        [Image] => 
        [Description] => o7thwd: finished up my GUI for a YUI minifier: http://t.co/7DKLoCXJHw
    )

[38] => Array
    (
        [Title] => o7thwd: o7th Web Design Articles http://t.co/QAIUO2Qjko
        [Link] => http://twitter.com/o7thwd/statuses/314356123027243008
        [PublishDate] => 3/20/2013 8:41:54 AM
        [Image] => 
        [Description] => o7thwd: o7th Web Design Articles http://t.co/QAIUO2Qjko
    )

[39] => Array
    (
        [Title] => SkorIn2: starting new desktop app to compliment the web app
        [Link] => http://twitter.com/SkorIn2/statuses/314718985725804545
        [PublishDate] => 3/21/2013 8:43:48 AM
        [Image] => 
        [Description] => SkorIn2: starting new desktop app to compliment the web app
    )

)

However, the array is not reversed, as you can tell above. Can anyone help me with this?
It's showing me that the array is indeed sorted by the 'PublishDate', however, in the reverse order that I need it in.

like image 929
Kevin Avatar asked Mar 21 '13 13:03

Kevin


People also ask

How do I sort a multidimensional array in PHP?

The array_multisort() function returns a sorted array. You can assign one or more arrays. The function sorts the first array, and the other arrays follow, then, if two or more values are the same, it sorts the next array, and so on.

How do you sort a multi dimensional array?

Sorting a multidimensional array by element containing date. Use the usort() function to sort the array. The usort() function is PHP builtin function that sorts a given array using user-defined comparison function. This function assigns new integral keys starting from zero to array elements.

How do you sort an associative array in PHP?

The arsort() function sorts an associative array in descending order, according to the value. Tip: Use the asort() function to sort an associative array in ascending order, according to the value. Tip: Use the krsort() function to sort an associative array in descending order, according to the key.

How do I sort a key in PHP?

To PHP sort array by key, you should use ksort() (for ascending order) or krsort() (for descending order). To PHP sort array by value, you will need functions asort() and arsort() (for ascending and descending orders).


1 Answers

The actual problem is you're not assigning the result of array_reverse to anything

// change this
array_reverse($respArr);

// to this
$respArr = array_reverse($respArr);
like image 137
Crisp Avatar answered Oct 13 '22 12:10

Crisp