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.

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,
);
  1. Sort numerical value using the sort command
  2. Multi-line string in Bash
  3. Create an array containing a range of elements in PHP
  4. Convert array to object in PHP
  5. Check if an array is associative or sequential in PHP?

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>