Home arrow PHP arrow Page 8 - Array Manipulation With PHP4

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

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: