PHP
  Home arrow PHP arrow Page 7 - Array Manipulation With PHP4
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Array Manipulation With PHP4
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 8
    2001-11-09

    Table of Contents:
  • Array Manipulation With PHP4
  • Having Your Cake
  • When Size Does Matter...
  • Push And Pull
  • Slice And Dice
  • Where Am I?
  • Sorting Things Out
  • Flipping Out

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Array Manipulation With PHP4 - Sorting Things Out


    (Page 7 of 8 )

    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


     

       

    PHP ARTICLES

    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP
    - PHP Networking
    - Validating Web Forms with the Code Igniter P...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT