PHP Questions Home

Categories

Arrays
Files
Forms
Functions
Images
MySQL
Numbers
Others
Strings
Website


PHP Functions

PHP Functions


More PHP

Top Questions
Ask a Question

How do I sort an array by key?


The key to sorting by key, is to add a k before the sort to get the function ksort.

This takes the name of the array as the parameter, and works like this:

<?php
$myworld 
= array("c"=>"better","a"=>"make","b"=>"me");
print_r($myworld);
?>


This prints:

Array
(
[c] => better
[a] => make
[b] => me
)

We need to use ksort to get the right key order:

<?php
$myworld 
= array("c"=>"better","a"=>"make","b"=>"me");
ksort($myworld);
print_r($myworld);
?>


Array
(
[a] => make
[b] => me
[c] => better
)



More arrays PHP Questions

How do I add to the beginning of an array and find the number of elements in it?
How do I sort the members of an array by value?
How do I check if a variable is of array type?
How do I remove and view the last element of an array?
How can I display values in a two dimensional array?