
The ArrayObject class in PHP allows to work with objects as arrays. Let’s look at the terminology of this class and the interfaces that it implements.
The ArrayObject class in PHP one of the built-in classes that enables to access objects as arrays. The class is widely in third-party PHP libraries that deal with arrays in an object manner like Database ORMS, collections, maps and so on.
ArrayObject Class Synopsis
class ArrayObject implements IteratorAggregate, ArrayAccess, Serializable, Countable { /* Constants */ public const int STD_PROP_LIST; public const int ARRAY_AS_PROPS; /* Methods */ public __construct(array|object $array = [], int $flags = 0, string $iteratorClass = ArrayIterator::class) public append(mixed $value): void public asort(int $flags = SORT_REGULAR): true public count(): int public exchangeArray(array|object $array): array public getArrayCopy(): array public offsetSet($key, $value): void public offsetGet($key): mixed public offserExists($key): bool .... .... }
ArrayObject implements many interfaces like the ArrayAccess, Countable, Serializable and IteratorAggregate and thereby you can use these interfaces methods in the class.
The class constructor accepts the array to work with and two optional arguments $flags and $iteratorClass.
Basic usage:
<?php $arr = [ 'PHP', 'Javascript', 'Python', 'C#' ]; $arrayObj = new ArrayObject($arr); echo $arrayObj[1]; // Javascript echo isset($arrayObj[2]) ? 'yes' : 'no'; // yes
I created a new instance of ArrayObject and passed the $arr. Then i accessed the elements in the $arrayObj instance like array using the array index index.Â
Also because the ArrayObject implements the ArrayClass interface you can access any array element in OOP syntax using offsetGet(), offsetSet() and offsetExists() methods:
$arrayObj->offsetSet(4, 'C++'); echo $arrayObj->offsetExists(4) ? 'exist' : 'not exist'; echo $arrayObj->offsetGet(4); // output // exist // C++
Appending elementsÂ
The ArrayObject::append() method append elements as the last value:
$arrayObj->append('Kotlin'); var_dump($arrayObj); echo $arrayObj[4]; // Kotlin
Sorting elementsÂ
The ArrayObject class provides various methods to sort the elements just like normal arrays, from these methods the asort(), ksort(), uksort(), uasort(), natsort(), natcasesort().Â
ArrayObject::asort(): Sort the entries by values in ascending order, maintain the keys correlation with values:
$arrayObj = new ArrayObject(['b' => 'frontend', 'c' => 'ui/ux', 'a' => 'backend', 'd' => 'mobile']); $arrayObj->asort(); foreach ($arrayObj as $key => $val) { echo "$key = $val\n"; } // output a = backend b = frontend d = mobile c = ui/ux
ArrayObject::ksort(): Sort the entries by keys, maintain keys to entry correlation:
$arrayObj = new ArrayObject(['b' => 'frontend', 'c' => 'ui/ux', 'a' => 'backend', 'd' => 'mobile']); $arrayObj->ksort(SORT_REGULAR); foreach ($arrayObj as $key => $val) { echo "$key = $val\n"; } // output a = backend b = frontend c = ui/ux d = mobile
ArrayObject::uasort(): Sort the entries according to a user defined callback:
$arrayObj->uasort(function($a, $b){ if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; }); foreach ($arrayObj as $key => $val) { echo "$key = $val\n"; }
The uasort() method accepts a callback. The callback takes two arguments and should return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
Retrieving Copy of the array
The ArrayObject::getArrayCopy() method return a copy of the array.
$arrayObj = new ArrayObject(['b' => 'frontend', 'c' => 'ui/ux', 'a' => 'backend', 'd' => 'mobile']); var_dump($arrayObj->getArrayCopy()); // output array(4) { ["b"]=> string(8) "frontend" ["c"]=> string(5) "ui/ux" ["a"]=> string(7) "backend" ["d"]=> string(6) "mobile" }
Example: Custom collection implementation using ArrayObject:
class Product { function __construct(public int $id, public string $title, public string $price) {} } class Collection extends ArrayObject { } $products = new Collection([ new Product(1, "Product 1", "$20"), new Product(2, "Product 2", "$100"), new Product(3, "Product 3", "$300"), ]); echo $products[1]->title; echo "\n" . $products->count(); var_dump($products[2]);