Visit the Official PHP Website

Search this Site

PHP Programming

By James N Hitz

PHP's array-manipulation Functions

PHP's in-built functions for manipulating arrays are many. These include:

  • sizeof
  • sort
  • list
  • next
  • current
  • prev
  • array_shift
  • array_unshift
  • array_pop
  • array_push

In the folowing examples we will be using PHP array manipulation arrays to manipulate the following array:

$fruits = array("mangoes","oranges","bananas","apples","pawpaws");

sizeof()

This is a function that returns (or gives back) the number of items (or 'rooms' if you'd rather stick to our analogy) in the array. For example:

$numOfFruits = sizeof($fruits);
print("$numOfFruits");

This outputs 5. This is because there are five 'rooms' in the array. It is a useful function since most of the times we will normally have no idea as to ho many items are there in an array.

sort()

sort() does exactly as the name suggests. It arranges the contents of an array in ascending order. The syntax is:

sort($array);

To sort the contents of our $fruits array we apply the command...

sort($fruits);

...and that's it! Of course there are other functions (too many if you ask me) for sorting arrays. This should suffice for now

next(), current(), and prev()

These are functions that are used to reference the next, current and previous rooms in the array respectively. What this means is that, if the array is being read and the array pointer happens to be pointing at "room" 3 then:

current($array) will reference room three,
prev($array) will reference room two and
next($array) will reference room four.

If you remember, we already used current() to traverse an array a few pages back. Another look at it may shed some light to the traversing of an array but... maybe not yet.

array_shift() and array_unshift()

array_shift is an array function that deletes the first item in the array and assigns it to a variable:

$firstItem = array_shift($array);

This is called shifting. In our $fruit example this code:

$firstFruit = array_shift($fruits); echo $firstFruit;

...would produce:

mangoes

Remember now mangoes have been deleted from array which thus far looks like this:

orangesbananasapplespawpaws

array_unshift
on the other hand does the opposite of array_shift funtion - it adds a new value in 'room 0' pushing all the values right thus this...

array_unshift($fruits, "guavas");

..would add a new item guava on the LEFT side of the array fruits now our array looks like this:

guavasorangesbananasapplespawpaws

This is called unshifting

array_pop() and array_push()

array_shift and array_unshift work on the left side of an array. array_pop and array_push on the other hand do the same job but on the right side of the array.

array_pop extracts the last value on the right hand side of an array and assigns it to a scalar variable; deleting it from the array. This...

$lastfruit = array_pop($fruits);

...extracts the last fruit (pawpaw) and assigns it to the scalar variable $lastfruit. The value is then erased from the array. Now our ochard array looks like this:

guavasorangesbananasapples

array_push on the other hand adds and item to the right hand side of the array. The following code snippet...

array_push($fruits, "avocado");

...adds a new entry to the end of the array. The array now looks like thus:

guavasorangesbananasapplesavocadoes

If you remember we said earlier that you can also add an item to the right hand side of the array like this:

$arrayName[] = "new value";

so that this code....

$fruits[] = "avocadoes";

...would have acheived the same things as array_push($fruits,"avocadoes");

array_push, array_pop, array_shift and array_unshift are only available in PHP version 4 and above. If you are using a lower version, please consider getting yourself a newer version of PHP from www.php.net

There are so many aspects of arrays that will slowly surface as we continue with our PHP-ing lessons. For now its good night. I recommend you take a break and reflect. We'll be seeing you in Lesson 4.

<< Traversing an Array | Lesson 4 >>

JamHitz Productions