Tuesday, 15 May 2012

PHP slice associative array -



PHP slice associative array -

this array

$array = array( "13111" => "2014-06-21 19:51:00.0000000", "23111" => "2014-06-20 19:51:00.0000000", "12111" => "2014-06-21 19:51:00.0000000", "23311" => "2014-06-22 19:51:00.0000000", "13114" => "2014-06-21 19:51:00.0000000", "23711" => "2014-06-20 19:51:00.0000000", );

how can first 3 elements of array , how can sort datetime? thanks

what want is:

sort($array); $array = array_slice($array, 0, 3);

first, sort function sort them lexicographically (which in case coincides date) , slice elements want.

edit

if want preserve keys use

asort($array); // "asort" instead of simple "sort" $array = array_slice($array, 0, 3, true); // note final "true" parameter!

php arrays

No comments:

Post a Comment