PHP Reference ยท Standard PHP Library and Classes
The ArrayObject and ArrayIterator classes
Reference for PHP ArrayObject and ArrayIterator: wrap an array in an object, use array syntax, count and foreach, sort in place, ARRAY_AS_PROPS and common pitfalls.
ArrayObject wraps an array (or an object's properties) in an object that behaves like the array: you can read and write with $obj['key'], count it with count() and loop over it with foreach. Because it is an object, it is passed by handle, can be extended, and can enforce rules in subclasses.
ArrayIterator is the iterator that ArrayObject uses internally. You can also use it on its own when you need a cursor over an array: it can seek() to a position, be sorted, and be wrapped by other iterators such as LimitIterator.
<?php $bag = new ArrayObject(['a' => 1, 'b' => 2]); $bag['c'] = 3; echo count($bag), ' ', $bag['b'];
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
class ArrayObject implements IteratorAggregate, ArrayAccess, Serializable, Countable { public function __construct(array|object $array = [], int $flags = 0, string $iteratorClass = ArrayIterator::class); } class ArrayIterator implements SeekableIterator, ArrayAccess, Serializable, Countable { public function __construct(array|object $array = [], int $flags = 0); }
| Member | Description |
|---|---|
$array |
The array or object to wrap. Objects expose their public properties. |
$flags |
ArrayObject::STD_PROP_LIST (normal property listing) and/or ArrayObject::ARRAY_AS_PROPS (array keys also readable as properties). |
append($value) |
Adds a value with the next integer key. |
getArrayCopy() |
Returns the data as a plain array. |
exchangeArray($array) |
Replaces the storage and returns the old array. |
count() |
Number of elements. |
asort(), ksort(), uasort(), uksort(), natsort(), natcasesort() |
Sort in place; the same behavior as the array functions of the same name. |
getIterator() |
Returns an ArrayIterator (ArrayObject only). |
seek($offset) |
Moves the cursor to a position (ArrayIterator only). |
Return value: constructors return the object. getArrayCopy() returns an array.
Errors / Exceptions: ArrayIterator::seek() throws OutOfBoundsException for a position that does not exist. Reading a missing key emits an Undefined array key warning, as with arrays.
Usage notes
- It is not an array. Functions such as
array_map(),in_array()orsort()do not accept it; callgetArrayCopy()first, or use the sort methods on the object. - Objects are shared.
$b = $amakes both variables point to the sameArrayObject; useclonefor a copy. - Nested writes such as
$obj['list'][] = 1work forArrayObject(it returns references to its storage), unlike a hand-writtenArrayAccessclass. - Wrapping an object exposes its public properties as elements and writes through to the object. Wrapping another
ArrayObjectis allowed. - Foreach while modifying: changing the storage during iteration can skip or repeat items; iterate over
getArrayCopy()when you need to modify. - Subclass it to add validation, for example overriding
offsetSet()to reject keys. Remember thatappend()and[]=both go throughoffsetSet(). - Use a plain array when nothing needs object behavior; it is faster and has more built-in functions.
- Typed collections are better built with your own
IteratorAggregateclass; see Iterator and IteratorAggregate.