Home arrow PHP arrow Page 4 - Array Manipulation With PHP4

Push And Pull - 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


You can add an element to the end of an existing array with the array_push() function,

<? // create array $superheroes = array("spiderman", "superman", "captain marvel", "green lantern"); array_push($superheroes, "the incredible hulk"); // $superheroes now contains ("spiderman", "superman", "captain marvel", "green lantern", "the incredible hulk") ?>
and remove an element from the end with the interestingly-named array_pop() function.

<? // create array $superheroes = array("spiderman", "superman", "captain marvel", "green lantern"); array_pop($superheroes); // $superheroes now contains ("spiderman", "superman", "captain marvel") ?>
If you need to pop an element off the top of the array, you can use the array_shift() function,

<? // create array $superheroes = array("spiderman", "superman", "captain marvel", "green lantern"); array_shift($superheroes); // $superheroes now contains ("superman", "captain marvel", "green lantern") ?>
while the array_unshift() function takes care of adding elements to the beginning of the array.

<? // create array $superheroes = array("spiderman", "superman", "captain marvel", "green lantern"); array_unshift($superheroes, "the human torch"); // $superheroes now contains ("the human torch", "spiderman", "superman", "captain marvel", "green lantern") ?>
In case you need an array of a specific length, you can use the array_pad() function to create an array and pad it with empty elements (or elements containing a user-defined value). The following example should make this clearer:

<? // create array $students = array(); // returns an array containing 4 null values $students = array_pad($students, 4, ""); ?>
The second argument also specifies the direction of padding, while the third argument to the function specifies the value to be used for the empty elements.

<? // create array $desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake", "apricot fritters"); // returns an array containing 8 elements // ("chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake", "apricot fritters", "dummy", "dummy", "dummy") // array is padded to the right since the size is positive $desserts = array_pad($desserts, 8, "dummy"); // this would return the same array, but padded to the left (note the negative size) // ("dummy", "dummy", "dummy", "chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake", "apricot fritters") $desserts = array_pad($desserts, -8, "dummy"); ?>

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