Home arrow PHP arrow Page 3 - Array Manipulation With PHP4

When Size Does Matter... - 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
Once an array has been created, it's time to use it. Within the context of an array, the most important (and commonly-used) function is the sizeof() function, which returns the size of (read: number of elements within) the array.

Here's an example:

<? // create array $desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake", "apricot fritters"); // returns 5 echo sizeof($desserts); // create array $movies = array("romance" => "Moulin Rouge", "epic" => "Gladiator", "action" => "Rambo", "horror" => "The Sixth Sense"); // returns 4 echo sizeof($movies); ?>
If you're using a hash, the array_keys() and array_values() functions come in handy to get a list of all the keys and values within the array.

<? // create array $starwars = array("princess" => "Leia", "teacher" => "Yoda", "new hope" => "Luke", "bad guy" => "Darth", "worse guy" => "The Emperor"); // returns the array ("princess", "teacher", "new hope", "bad guy", "worse guy") array_keys($starwars); // returns the array ("Leia", "Yoda", "Luke", "Darth", "The Emperor") array_values($starwars); ?>
And the in_array() function can tell you whether or not a particular value exists in an array.

<? // create array $starwars = array("princess" => "Leia", "teacher" => "Yoda", "new hope" => "Luke", "bad guy" => "Darth", "worse guy" => "The Emperor"); // returns true echo in_array("Yoda", $starwars); // returns false echo in_array("Obi-Wan", $starwars); ?>
An alternative (and sometimes more efficient) approach to the one above would be to scan the array for a value match and return the corresponding key. If this is what you need, consider the array_search() function, which is designed just for this purpose:

<? // create array $starwars = array("princess" => "Leia", "teacher" => "Yoda", "new hope" => "Luke", "bad guy" => "Darth", "worse guy" => "The Emperor"); // returns "bad guy" echo array_search("Darth", $starwars); ?>
You can convert array elements into regular PHP variables with the list() and extract() functions. The list() function assigns array elements to variables,

<? // create array $desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake", "apricot fritters"); // assign elements to variables list($a, $b, $c, $d, $e) = $desserts; // returns "tiramisu" echo $b; ?>
while the extract() function iterates through a hash, converting the key-value pairs into corresponding variable-value pairs.

<? // create array $starwars = array("princess" => "Leia", "teacher" => "Yoda"); // assign elements to variables extract($starwars); // returns "Leia" echo $princess; ?>


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