PHP syntax: sort an associative array by index keys

Contributor Icon Contributed by qmchenry  
Tag Icon Tagged: PHP programming  

To sort an associative array by index values (or keys), use ksort.

ksort($capitals);

An optional parameter (sort_flags) can be passed to ksort to alter the way that it sorts the array. The following code will sort an associative array numerically by index.

ksort($ar, SORT_NUMERIC);

Without this option, the index values ‘15, 2, 1′ would be sorted ‘1, 15, 2′ and with the SORT_NUMERIC option, the values would be sorted 1, 2, 15.

 

1 Comment -


  1. Arun Jain said on January 12, 2012

    Really great what i expected. I always welcome to learn new things. Thanks

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -