php array object class

PHP Array Object

PHP array object class allows objects to work as arrays. Array does not object in PHP but PHP does give us some ways to make objects that act like arrays. First, there is the ArrayObject class which is pretty close to what you have with a normal array (you can iterate it, use [], etc.)

$cars = new ArrayObject(['fer' => 'Ferrari', 'ben' => 'Benz', 'bmw' => 'BMW']);

$cars['vol'] = 'Volvo';

foreach ($cars as $key => $car) {
    echo $key . " <=> " . $car . "</br>";
}

Output

fer <=> Ferrari
ben <=> Benz
bmw <=> BMW
vol <=> Volvo

PHP ArrayObject class

The ArrayObject is a class that you can extend to create objects that behave as if they were arrays. It implements methods like count and sort that enable you to treat an object like you would treat an array. It’s part of the SPL (Standard PHP Library).

We use this class to treat objects like arrays. But array functions like sort() don’t support ArrayObject class. Then it has its own functions to do so.

$cars = new ArrayObject(['fer' => 'Ferrari', 'ben' => 'Benz', 'bmw' => 'BMW', 'sub' => 'Subaru']);

$cars->append('Volvo');
$cars->offsetSet('toy', 'Toyota');
$cars->offsetUnset('sub');

$cars->asort();

var_dump($cars);

echo $cars->count() . "</br>";
echo $cars->offsetExists('bmw') . "</br>";
echo $cars->offsetGet('bmw') . "</br>";

$copy_cars = $cars->getArrayCopy();

var_dump($copy_cars);

Output

object(ArrayObject)[1]
  private 'storage' => 
    array (size=5)
      'bmw' => string 'BMW' (length=3)
      'ben' => string 'Benz' (length=4)
      'fer' => string 'Ferrari' (length=7)
      'toy' => string 'Toyota' (length=6)
      0 => string 'Volvo' (length=5)

5
1
BMW

array (size=5)
  'bmw' => string 'BMW' (length=3)
  'ben' => string 'Benz' (length=4)
  'fer' => string 'Ferrari' (length=7)
  'toy' => string 'Toyota' (length=6)
  0 => string 'Volvo' (length=5)

As you can see there are functions like array functions in ArrayObject class in PHP.

$cars->append('Volvo');

Append and add an item to the class but you can’t add an item with the key.

$cars->offsetSet('toy', 'Toyota');

It is similar to append but with this function, you can add an item with a key.

$cars->offsetUnset('sub');

Delete an item from the class.

$cars->asort();

You can sort the values. ksort(), natcasesort(), natsort(), uasort() and uksort() functions are available too.

echo $cars->count() . "</br>";

Get ArrayObject length and count.

echo $cars->offsetExists('bmw') . "</br>";

Search the given key in the ArrayObject, if it exists return 1 and if not return nothing (empty).

echo $cars->offsetGet('bmw') . "</br>";

Returns the value of the given key.

$copy_cars = $cars->getArrayCopy();

Gets a copy of the given ArrayObject as an array.

More functions and details of them are available on the PHP website.

PHP Array vs Array Object

In terms of performance, you won’t notice a real difference between an array and a ArayObject. The ArrayObject needs a little more effort to deal with. But it is most useful when serialization is needed.

Extend ArrayObject class

Array Object could be extended and functions are overridden. For example, your append() function could format a number to two decimal places before calling parent::append()

class MyArrObj extends ArrayObject
{
    public function append(mixed $value): void
    {
        //code goes here
    }
}

You may like

Shopping Cart