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 an array of numbers and many repeat, how to list all items in order of maximum occurence?

what are php array functions?

I want to invert my value in array how can I do this?

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

when i fetch the record in the database but they skip first record in the database



Custom Search