PHP
  Home arrow PHP arrow Page 8 - 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 - Flipping Out
    ( Page 8 of 8 )

    A number of other functions are available to help you perform miscellaneous array manipulation. For example, the array_flip() function can be used to interchange keys and values in an array,

    <? // create array $music = array("pop" => "Britney Spears", "rock" => "Aerosmith", "jazz" => "Louis Armstrong"); // returns a new array ("Britney Spears" => "pop", "Aerosmith" => "rock", "Louis Armstrong" => "jazz") array_flip($music); ?>
    while the array_merge() function combines multiple arrays into one single array.

    <? // create arrays $good_guys = array("princess" => "Leia", "teacher" => "Yoda", "new hope" => "Luke"); $bad_guys = array("bad guy" => "Darth", "worse guy" => "The Emperor"); // returns a combined array("princess => Leia", "teacher => Yoda", "new hope => Luke", "bad guy => Darth", "worse guy => The Emperor") $starwars = array_merge($good_guys, $bad_guys); ?>
    The array_diff() function accepts two or more arrays as arguments, and returns an array containing all those values of the first array which are absent in the remaining ones,

    <? // create arrays $subset = array(7, 14, 21, 28); $list = array(1, 4, 7, 8, 0, 23, 45, 15, 67, 29, 22); // returns ("14", "21", "28") array_diff($subset, $list); ?>
    while the array_intersect() function does the opposite, calculating a list of those values of the first array which are present in all the remaining ones.

    <? // create arrays $subset = array(7, 14, 21, 28); $list = array(1, 4, 7, 8, 0, 23, 45, 15, 67, 29, 22); // returns ("7") array_intersect($subset, $list); ?>
    The array_sum() function adds all the elements of an array and returns the total,

    <? // create an array $subset = array(7, 14, 21, 28); // returns 70 echo array_sum($subset); ?>
    while the array_count_values() function calculates the frequency with which values appear within an array.

    <? // create array $clones = array("Tom", "Tom", "Harry", "Tom", "Harry", "Harry", "Harry", "Tom", "Frank"); // returns the hash ("Tom => 4", "Harry => 4", "Frank => 1") array_count_values($clones); ?>
    The array_rand() function randomly returns one or more keys from an array.

    <? // create array $desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake", "apricot fritters"); // returns the random array (1, 3) array_rand($desserts, 2); // create array $starwars = array("princess" => "Leia", "teacher" => "Yoda", "new hope" => "Luke", "bad guy" => "Darth", "worse guy" => "The Emperor"); // returns the random array ("princess", "bad guy") array_rand($starwars, 2); ?>
    And finally, the very useful array_walk() function allows you to run a user-defined function on every element of an array. The following example uses it to apply a specific number format to all the numbers in an array:

    <? // create arrays $numbers = array(1, 567, 1.6777777777777, 0.031, 100.1, -98.6); $new_numbers = array(); // function to format numbers and add them to new array function format($num) { global $new_numbers; $new_numbers[] = sprintf("%1.2f", $num); } // runs the function format() on every element of $numbers array_walk($numbers, "format"); // $new_numbers now contains ("1.00", "567.00", "1.68", "0.03", "100.10", "-98.60") ?>
    And that's about all I have. I hope you enjoyed this article, and that it offered you some insight into the massive amount of power at your disposal when it comes to manipulating arrays. Should you require more information, the best place to go is the PHP manual page on arrays at http://www.php.net/manual/en/ref.array.php

    Take care, and I'll see you soon!

    Note: All examples in this article have been tested on Linux/i586 with PHP 4.0.6. Examples are illustrative only, and are not meant for a production environment. YMMV!

     
     
    >>> 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