PHP Questions Home
Categories
Arrays
Files
Forms
Creating PDFs
PHP Errors
Functions
Images
MySQL
Numbers
Others
Strings
Website

PHP Forums
Forums Index
MySQL Forum
PHP Forum
Flash/Actionscript
Javascript
Web design

More PHP
PHP Functions
Top Questions
Ask a Question
PHP Interview Questions
PHP Jobs
Ecommerce
IT Questions
Business Websites

Sponsors

sponsor ad
My Salary
PHP Developer Salary
Advertise Here

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
)



ASK A QUESTION

More arrays PHP Questions

I have the following list : Tom T1 Tom T2 Bill T1 Bill T2 Jack T1 Jack T2 I need it converted into : Tom T1,T2 Bill T1,T2 Jack T1,T2

How do I remove the first element from an array?

How can I mix up the order of values in an array?

How can I reset an array in PHP?

How do I generate an associative array like Jan => 10, Feb =>20 etc from a mysql table of 2 columns, month and val ?



Custom Search