Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Jul 112010
 

Sort an array by key in PHP

Answer:

Assume you have an array in PHP as following,

$a = array (
    "orange" => 3,
    "mango" => 2,
    "apple" => 1,
);

You can sort the array by the key, with the ksort function

ksort($a);

Then the array become:

(
    "apple" => 1,
    "mango" => 2,
    "orange" => 3,
);

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>