ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

Back to basics: PHP & Arrays

(Page 3 out of 3)

Sorting Arrays

At some point you might want to sort an array a certain way, say alphabetical or lowest-highest (or the other way round). Thankfully, this is very easy, as PHP comes with several inbuilt sorting functions:

sort()
This is the simplest sort function, and can be used to sort an array alphabetically or from lowest to highest. It works like this:


$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
?>

The fruits arrays will now look like this: 'apple', 'banana', 'lemon', 'orange'.

rsort()
This function is exactly like the sort() function, except it does it in reverse order, so from z-a or from the highest to lowest. If we used rsort in the code above, the fruits array would then look like this: 'orange', 'lemon', 'banana', 'apple'.

asort()
If you try to sort an associate index, using the sort() function, you will probably discover that the keys have been lost. Don't worry, as this function maintains the keys and will still sort the array. It's used like this:


$fruits = array('one' => "lemon", 'two' => "orange", 'three' => "banana", 'four' => "apple");
asort($fruits);
?>

The fruits array will now look like this: 'four' => "apple", 'three' => "banana",'one' => "lemon",'two' => "orange").

arsort()
You can probably guess what this function does: it's just like the rsort() function, except it maintains the item keys.

ksort()
This function can be used to sort an array by key, instead of by value (like the previous functions). It's used like so:


$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
?>

The fruits array now looks like this: "a" => "orange", "b" => "banana", "c" => "apple", "d" => "lemon"

krsort()
It's what you think it is: the same as the ksort() function except it sorts in a reverse order.

natsort()
This is actually a bit of a vague function, but it sorts items using a Natural Order algorithm (and maintains item keys). More information on the Natural Order algorithm can be found at http://sourcefrog.net/projects/natsort/, but it's basically a smart way of sorting. For example, how would you sort the following: img10.jpg, img12.jpg, img1.jpg, img2.jpg. Probably like this: img1.jpg, img2.jpg, img10.jpg, img.12 which is quite logical to humans, but the sort() function would sort it like this: img1.jpg, img10.jpg, img12.jpg, img2.jpg, since img2 comes after img1 (including img12 and img10).

These are the most important array sorting functions, and can be very useful at times, but don't overuse them. If you're getting data from a database, you'll probably won't want to use an array sorting function, but instead add the sorting code to your SQL query (ORDER BY field).

Conclusion

In this article I have taken you through the basics of arrays in PHP, starting with an explanation of what they are and how to use them, and then ending with a way to sort them. I have also shown you two ways to loop through an array.

Arrays might seem a bit daunting in the beginning, especially if you don't have any programming background at all, but learn them, use them, and love them. They are one of the best things of PHP, and almost any programming language has arrays, which just goes to show that they are (almost) the best thing since sliced bread.

If you have any comments, questions or simply need help, post in the comments below, or head on over to the PHPit Forums where you can get any help you need with arrays, variables or any other PHP topic.

« Previous: Commonly Used Functions



5 Responses to “Back to basics: PHP & Arrays”

  1. OAKA Blog » Blog Archive » Using Arrays in PHP Says:

    […] Site: http://phpit.net/article/back-to-basics-arrays/1/ […]

  2. CubSpeaking » Using Arrays in PHP Says:

    […] Site: http://phpit.net/article/back-to-basics-arrays/1/ […]

  3. Bocse L Filip Says:

    The natural algorythm is not only the human-like ordering algo. If a machine would see strings the way we do the machines would also naturally sort in the same manner as humans…. only difference is the representation of a string.
    Of course we see strings as a set of various characters different by shape, meaning and so on… not the machines though… these only see 1’s and 0’s .It is true these beautifull machines have the ability to represent these 1’s and 0’s in a human understandable representation but it is just like a translation from their language (1’s and 0’s ) to a language which they do not understand… based on a strict dictionary.
    It is sad :) but true… the machines have no imagination. May one teach a machine what imagination is it is very likely our natural algo will seem redundant to whatever these machines may consider “natural algo”.

  4. Secret Black Book of IceFuzion » Back to basics: PHP & Arrays Says:

    […] read more | digg story […]

  5. Ocus Says:

    Playing The Angel

    PHPit - Totally PHP &r…

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & Basics
  2. Commonly Used Functions
  3. Sorting Functions
Bookmark Article
Download Article
PDF
Download this article as a PDF file