$array = new ArrayObject(['foo', 'bar', 'baz']); $array->offsetUnset(1); print_r($array);
ArrayObject Object ( [0] => foo [2] => baz )
$array = new ArrayObject(['apple', 'banana', 'cherry', 'durian', 'elderberry']); $keys_to_remove = [1, 3]; foreach ($keys_to_remove as $key) { $array->offsetUnset($key); } print_r($array);
ArrayObject Object ( [0] => apple [2] => cherry [3] => elderberry )In this example, we used offsetUnset() in a loop to remove multiple elements from the array based on their index. We first created our ArrayObject and then defined an array of keys to remove. We then looped through each key and called offsetUnset() on the ArrayObject, passing the current key as an argument. Finally, we printed the resulting ArrayObject, which contains only the elements that were not removed. Package library: Core PHP