Search code examples
phparrayssorting

Sort an array by its keys ascending


I have this array

Array (  
 [146] => Post Object ( 
 [ID] => 5664  
 [post_author] => 8  
 [post_date] => 2016-04-10 16:21:55  
 [post_content] =>
 )  

 [145] => Post Object (  
 [ID] => 5638  
 [post_author] => 7  
 [post_date] => 2016-03-17 12:23:00  
 [post_content] =>
 )
) 

I want to order my array by the numbers before "=> Post Object".

i.e. I want that the sub-array 145 appear before 146.


Solution

  • You can use ksort function.

    ksort($array);
    

    For others behaviour read the related documentation at http://php.net/manual/en/array.sorting.php