Jump to content

Sorting date in php array


peterjc

Recommended Posts

There are php sort() function to sort array.  But the problem that i face is how to sort the two-dimensional array and sort the date in desending order?  the array is as below

 


$data = array();

$data[0][0] = "2008-05-23";
$data[0][1] = "data 1";
$data[1][0] = "2008-05-15";
$data[1][1] = "data 2";
$data[2][0] = "2008-06-06";
$data[2][1] = "data 3";
$data[3][0] = "2008-05-08";
$data[3][1] = "data 4";

 

Any help is appreciated.  Thank in advance.

Link to comment
https://forums.phpfreaks.com/topic/109148-sorting-date-in-php-array/
Share on other sites

What are you actually asking?  You want the whole array to be sorted based on the date?

 

Ie:

$data[0][0] = "2008-05-23";

$data[0][1] = "data 1";

$data[1][0] = "2008-05-15";

$data[1][1] = "data 2";

$data[2][0] = "2008-06-06";

$data[2][1] = "data 3";

$data[3][0] = "2008-05-08";

$data[3][1] = "data 4";

 

Becomes:

$data[0][0] = "2008-05-08";

$data[0][1] = "data 4";

$data[1][0] = "2008-05-15";

$data[1][1] = "data 2";

$data[2][0] = "2008-05-23";

$data[2][1] = "data 1";

$data[3][0] = "2008-06-06";

$data[3][1] = "data 3";

 

array_multisort($data, SORT_DESC);

echo "<pre>";
print_r($data);
echo "</pre>";

 

This basically sorts by the first dimension in the array. This operates similar to a ORDER BY clause in a sql query. Check the array_multisort section in the manual for more.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.