Home arrow PHP arrow Page 7 - Array Manipulation With PHP4

Sorting Things Out - PHP

This may come as a bit of a shock to you, but PHP's arrayfunctions can do a lot more than just count the elements of an array oriterate through key-value pairs. This article takes an in-depth look atPHP's less well-known array manipulation tools, illustrating how they canbe used to write tighter, more efficient code...and have some fun in thebargain as well!

TABLE OF CONTENTS:
  1. Array Manipulation With PHP4
  2. Having Your Cake
  3. When Size Does Matter...
  4. Push And Pull
  5. Slice And Dice
  6. Where Am I?
  7. Sorting Things Out
  8. Flipping Out
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 8
November 09, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
If you need to, you can rearrange the elements within an array with PHP's numerous sorting functions. The simplest of these is the array_reverse() function, which merely reverses the order of elements within an array.

<? // create array $trio = array("huey", "dewey", "louie"); // returns ("louie", "dewey", "huey") array_reverse($trio); ?>
The shuffle() function randomly reshuffles the elements within an array,

<? // create array $trio = array("huey", "dewey", "louie"); // shuffles elements of array to return ("dewey", "louie", "huey") shuffle($trio); ?>
while the array_unique() function helps you strip out duplicate values from an array.

<? // create array $clones = array("Tom", "Tom", "Harry", "Tom", "Harry", "Harry", "Harry", "Tom"); // returns ("Tom", "Harry") array_unique($clones); ?>
The sort() function can be used to sort an array alphabetically or numerically,

<? // create array $animals = array("antelope", "zebra", "skunk", "baboon", "viper"); // returns ("antelope", "baboon", "skunk", "viper", "zebra") sort($animals); ?>
while the rsort() function does the same thing (just the other way around).

<? // create array $animals = array("antelope", "zebra", "skunk", "baboon", "viper"); // returns ("zebra", "viper", "skunk", "baboon", "antelope") rsort($animals); ?>
The ksort() function sorts a hash by key (you can reverse the sort order with the krsort() function),

<? // create array $numbers = array("2" => "duet", "13" => "baker's dozen", "-15" => "temperature", "3" => "stooges"); // returns ("-15" => "temperature", "2" => "duet", "3" => "stooges", "13" => "baker's dozen") ksort($numbers); // returns ("13" => "baker's dozen", "3" => "stooges", "2" => "duet", "-15" => "temperature") krsort($numbers); ?>
The usort() function lets you apply your own sort function to the elements of an array. The function that you define must be capable of comparing two values, and must return a positive, negative or zero value depending on whether the first value being compared is greater than, less than or equal to the second value.

An example might help to make this clearer. The following code snippet defines a custom sort function, which arranges elements according to their length.

<? // compare two values on length function check_length($str1, $str2) { if (strlen($str1) > strlen($str2)) { return 1; } elseif (strlen($str1) == strlen($str2)) { return 0; } else { return -1; } } // create array $animals = array("antelope", "zebra", "skunk", "baboon", "viper", "yak"); // returns ("yak", "skunk", "viper", "zebra", "baboon", "antelope") usort($animals, "check_length"); ?>
In a similar manner, you can apply a user-defined comparison function to the keys of a hash with the uksort() function - I'll leave this to you to experiment with.

The natsort() function makes it possible to sort array elements the way a human - rather than a computer - would. Consider the following example:

<? // create array $mixed = array(1, "zebra", "skunk", "baboon", 13, "viper", -99, "yak"); // returns ("-99", "baboon", "skunk", "viper", "yak", "zebra", "1", "13") sort($mixed); // returns ("-99", "1", "13", "baboon", "skunk", "viper", "yak", "zebra") natsort($mixed); ?>


 
 
>>> More PHP Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: