Home arrow PHP arrow Page 2 - The Standard PHP Library, Part 2

The Array Object - PHP

In our second article on the standard PHP library, David Fells explains the new Arrray object, introduces Iterators and the ArrayIterator, and discusses some practical examples of their usage. This article assumes a knowledge of how Exceptions work in PHP 5.

  1. The Standard PHP Library, Part 2
  2. The Array Object
  3. Simple Array Iterators
  4. Directory Iteration
  5. Recursive Iteration
By: David Fells
Rating: starstarstarstarstar / 18
December 14, 2004

print this article



Programmers familiar with Java and .NET will be at home with using the new ArrayObject class. The ArrayObject class has a definition like this:

class ArrayObject implements IteratorAggregate, 
ArrayAccess, Countable { public function __construct($array); public function append($value); public function count(); public function getArrayCopy(); public function getIterator(); public function offsetExists($index); public function offsetGet($index); public function offsetSet($index, $newval); public function offsetUnset($index); }

As you see in the declaration, the ArrayObject class implements three interfaces. The Countable interface requires the public method count() to be defined and allows the built in PHP count() function to be used on the object. The ArrayAccess interface is used to override array access of objects. For example, the offsetGet($index) method is used in place of the normal array style: $array[$index]. This interfaces calls for the public methods offsetExists(), offsetGet(), offsetSet() and offsetUnset(). The IteratorAggregate interface requires the public method getIterator(), which allows the calling code to use an external iterator for object traversal.

It is worth noting that the IteratorAggregate interface implements the Traversable abstract base interface, which is used to detect if a class is traversable using foreach. This interface must be implemented by either Iterator (an iterator object) or IteratorAggregate (an object to be iterated).

Let's take a look at some sample code:

$myArray = new ArrayObject();
echo 'First Element: '.$myArray->offsetGet(0).'<br />';
echo 'Second Element: '.$myArray->offsetGet(1).'<br />';
echo 'Third Element: '.$myArray->offsetGet(2).'<br />';
echo 'Number of Elements: '.$myArray->count().'<br />';
$myArray->offsetSet(1, 'a');
echo 'First Element: '.$myArray->offsetGet(0).'<br />';
echo 'Second Element: '.$myArray->offsetGet(1).'<br />';
echo 'Third Element: '.$myArray->offsetGet(2).'<br />';
echo 'Number of Elements: '.$myArray->count().'<br />';

The first line creates the ArrayObject object and the next three lines store the values "a", "b" and "c'' in it. The next four lines demonstrate using the offsetGet() method and the count() method to retrieve values at a specified offset and to return the number of elements in the array, respectively. Next we unset the first index value by calling offsetUnset() and then change the value of the second index to "a". We then repeat the process of outputting the values in the array and the number of elements in the array. This script will output the following:

First Element: a
Second Element: b
Third Element: c
Number of Elements: 3
First Element: 
Second Element: a
Third Element: c
Number of Elements: 2

Note that after calling unset, the index at position 0 still exists but has no value and is not included when calling count(). This seems like a counterintuitive behavior. It seems that the expected result would have been to completely remove the element - index and all - from the array and shift the other elements down. The term unset implies that after calling the method, the value is no longer set at all - meaning it is not set to NULL, 0, '' or anything else, and the index does not exist in the array. I would be interested to hear readers' thoughts on this, as I see it as poor implementation.

>>> More PHP Articles          >>> More By David Fells

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: