Home arrow PHP arrow Page 6 - Array Manipulation With PHP4

Where Am I? - 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 built-in functions are available for use while iterating through an array - you'll typically use these in combination with a "while" or "for" loop.

The current() function returns the currently-in-use element of an array (beginning with the first element),

<? // create array $friends = array("Rachel", "Monica", "Phoebe", "Joey", "Chandler", "Ross"); // returns "Rachel" echo current($friends); ?>
while the key() function returns the corresponding array key.

<? // create array $friends = array("Rachel", "Monica", "Phoebe", "Joey", "Chandler", "Ross"); // returns "Rachel" echo current($friends); // returns 0 echo key($friends); ?>
The next() and prev() functions move forward and backward through the array.

<? // create array $friends = array("Rachel", "Monica", "Phoebe", "Joey", "Chandler", "Ross"); // returns "Rachel" echo current($friends); // move forward next($friends); // returns "Monica" echo current($friends); // move forward next($friends); // returns "Phoebe" echo current($friends); // move backwards prev($friends); // returns "Monica" echo current($friends); ?>
The reset() and end() functions move to the beginning and end of an array respectively,

<? // create array $friends = array("Rachel", "Monica", "Phoebe", "Joey", "Chandler", "Ross"); // returns "Rachel" echo current($friends); // move to end end($friends); // returns "Ross" echo current($friends); // move forward reset($friends); // returns "Rachel" echo current($friends); ?>
while the each() function comes in handy when you need to iterate through an array.

<? // create array $music = array("pop", "rock", "jazz", "blues"); // creates the array ("0" => 0, "1" => "pop", "key" => 0, "value" => "pop") $this = each($music); // returns "0" - no key, as this is not a hash echo $this["0"]; // returns "pop" echo $this["1"]; // returns "0" - no key, as this is not a hash echo $this["key"]; // returns "pop" echo $this["value"]; ?>
Confused? Don't be - every time each() runs on an array, it creates a hash containing four keys: "0", "1", "key" and "value". The "0" and "key" keys contain the name of the currently-in-use key of the array (or hold the value 0 if the array does not contain keys), while the "1" and "value" keys contain the corresponding value.

Here's another example to make this clearer:

<? // create array $music = array("pop" => "Britney Spears", "rock" => "Aerosmith", "jazz" => "Louis Armstrong"); // creates the array ("0" => "pop", "1" => "Britney Spears", "key" => "pop", "value" => "Britney Spears") $this = each($music); // returns "pop" echo $this["0"]; // returns "Britney Spears" echo $this["1"]; // returns "pop" echo $this["key"]; // returns "Britney Spears" echo $this["value"]; // move forward // creates the array ("0" => "rock", "1" => "Aerosmith", "key" => "rock", "value" => "Aerosmith") $this = each($music); ?>


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