Array_slice (or other array_ * functions) in ArrayObject

I have a question regarding ArrayObject. I wanted to use array_slice in an ArrayObject class, and I could not. Is there a way to do this without having to write a β€œslice” method for a class that implements ArrayObject?

+7
source share
2 answers

Having a class that wraps php array functions is not so bad. Will make the code much cleaner.

echo $myAry->slice(10, 5)->reverse()->join(", "); 

Like an ordinary language, you know.

+1
source

You can always work with a copy of the array:

 $array = $object->getArrayCopy(); // modify $array as needed, eg array_slice(....) $object = new ArrayObject($array); 

In the past, the idea was to make all the functions that take arrays (or probably many of them) to accept an ArrayObject. But I don’t know how far this went, and if it did follow. I think ArrayObject is more of a behavioral thing than replacing an existing array with PHP.

Related question: PHP Array and ArrayObject

+4
source

All Articles