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 change: function wordlimit($string, $length = 50, $ellipsis = "...") { $words = explode(' ', strip_tags($string)); if (count($words) > $length) return implode(' ', array_slice($words, 0, $length)) . $ellipsis; else

How can I create an array of numbers easily?

How can I create an array of the letters of the alphabet?

How can I reset an array in PHP?

How do I check if a variable is of array type?



Custom Search