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

How do I count the number of times a value appears in an array?

How do I reverse the order of the elements in an array?

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

OK I've been trying to echo out the arrays "$people and $other people" using the for-each method in alphabetical order. but when i see what this displays it does it in some other order. THIS IS THE CODE PHP Ch8 Lab

How can I loop through the members of an array?



Custom Search