Home arrow PHP arrow Page 2 - Array Manipulation With PHP4

Having Your Cake - 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!

  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


As you may have already guessed, defining an array variable takes place via the array() function - here's how:

<? $desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake"); ?>
The rules for choosing an array variable name are the same as those for any other PHP variable - it must begin with a letter, and can optionally be followed by more letters and numbers.

Alternatively, you can define an array by specifying values for each element in the index notation, like this:

<? $desserts[0] = "chocolate mousse"; $desserts[1] = "tiramisu"; $desserts[2] = "apple pie"; $desserts[3] = "chocolate fudge cake"; ?>
Incidentally, you can omit the index numbers if you prefer - the following snippet is equivalent to the one above:

<? $desserts[] = "chocolate mousse"; $desserts[] = "tiramisu"; $desserts[] = "apple pie"; $desserts[] = "chocolate fudge cake"; ?>
If you'd prefer to use keys instead of numeric indices, the following examples should make you happier:

<? $movies = array("romance" => "Moulin Rouge", "epic" => "Gladiator", "action" => "The Terminator"); // this is equivalent $movies["romance"] = "Moulin Rouge"; $movies["epic"] = "Gladiator"; $movies["action"] = "The Terminator"; ?>
You can use the range() function to automatically create an array containing a range of elements:

<? // returns the array ("30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40") $thirties = range(30, 40); // returns the array ("i", "j", "k", "l", "m", "n", "o") $alphabet = range("i", "o"); ?>
You can add elements to the array in a similar manner - for example, if you wanted to add the element "apricot fritters" to the $desserts array, you would use this:

<? $desserts[4] = "apricot fritters"; ?>
and the array would now look like this:

<? $desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake", "apricot fritters"); ?>
The same goes for non-numeric keys - adding a new key to the $movies array

<? $movies["horror"] = "The Sixth Sense"; ?>
alters it to read:

<? $movies = array("romance" => "Moulin Rouge", "epic" => "Gladiator", "action" => "The Terminator", "horror" => "The Sixth Sense"); ?>
In order to modify an element of an array, you would simply assign a new value to the corresponding scalar variable. If you wanted to replace "chocolate fudge cake" with "chocolate chip cookies", you'd simply use

<? $desserts[3] = "chocolate chip cookies"; ?>
and the array would now read

<? $desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate chip cookies", "apricot fritters"); ?>
You can do the same with named keys - the statement

<? $movies["action"] = "Rambo"; ?>
further alters the $movies array to

<? $movies = array("romance" => "Moulin Rouge", "epic" => "Gladiator", "action" => "Rambo", "horror" => "The Sixth Sense"); ?>
If it walks like an array, talks like an array and looks like an array, it must be an array...right? Well, you can always verify your suspicions with the is_array() function, which comes in handy if you need to test whether a particular variable is an array or not.

<? // create array $desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake", "apricot fritters"); // returns true echo is_array($desserts); $desserts = "blueberry ice cream"; // returns false echo is_array($desserts); ?>

>>> More PHP Articles          >>> More By Vikram Vaswani, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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