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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT