Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

ArrayObject

An object that behaves like an array.

Syntax

new ArrayObject(array|object $array = [])

Parameters

ParameterTypeDescription
$arrayarray|objectOptional. 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, juice

Notes

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.

Thanks for reading!

Donate to the site — we work hard for you.