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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
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 - 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!
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway