ArrayObject
An object that behaves like an array.
Syntax
new ArrayObject(array|object $array = [])Parameters
| Parameter | Type | Description |
|---|---|---|
| $array | array|object | Optional. The initial data. |
Return value
ArrayObjectAn object with square-bracket access, iteration and counting.
Examples
An object instead of an array:
<?php
$list = new ArrayObject(['tea', 'coffee']);
$list[] = 'juice';
echo count($list), ' ', $list[2], "\n";
echo implode(', ', $list->getArrayCopy());Output:
3 juice
tea, coffee, juiceNotes
Unlike an array, the object is passed to functions by handle: changes made inside a function are visible outside.
See also
- iterator_to_array — Turns an iterator or a generator into an array.
- ArrayAccess — The interface of objects accessed with square brackets.
- count — Returns the number of elements in an array.
In the tutorial
- Built-in interfaces — PHP interfaces that teach an object to behave like an array — Countable, IteratorAggregate, ArrayAccess, JsonSerializable and Stringable.