Sorting a multidimensional array in PHP?

arrays, multidimensional-array, php, sorting

Solution

use a compare function, in this case it compares the array's unix timestamp value:

function compare($x, $y) {
if ( $x[4] == $y[4] )
return 0;
else if ( $x[4] < $y[4] )
return -1;
else
return 1;
}

and then call it with using the `usort` function like this:

usort($nameOfArray, 'compare');

This function will sort an array by its values using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function.

Taken from PHP: usort manual.

Problem

I have this kind of an array ``` array(5) { [0]=> array(5) { [0]=> string(7) "jannala" [1]=> string(10) "2009-11-16" [2]=> string(29) " <p>Jotain mukavaa.</p> " [3]=> int(12) [4]=> int(1270929600) } [1]=> array(5) { [0]=> string(7) "jannala" [1]=> string(10) "2009-11-16" [2]=> string(51) " <p>Saapumiserä II/09 astuu palvelukseen</p> " [3]=> int(11) [4]=> int(1270929600) } ... } ``` What I need to be done is to sort the array based on array's [x][4] (the unix timestamp value). How would I achieve this?

Original source

Related problems