Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array_slice (or other array_* functions) on ArrayObject

I have a question regarding ArrayObject. I wanted to use array_slice in an ArrayObject class and I couldn't. Is there a way to do it, without needing to write an "slice" method to the class that implements ArrayObject?

like image 297
pocesar Avatar asked Jul 08 '11 16:07

pocesar


People also ask

What is the use of Array_slice () function?

The array_slice() function returns selected parts of an array. Note: If the array have string keys, the returned array will always preserve the keys (See example 4).

What are the parameters required in Array_slice?

Parameters: This function can take four parameters and are described below: $array (mandatory): This parameter refers to the original array, we want to slice. $start_point (mandatory): This parameter refers to the starting position of the array from where the slicing need to be performed.


1 Answers

You can always work on the array copy:

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

There sometime in the past was the idea to make all functions that accept arrays (or probably many of them) to accept ArrayObject as well. But I dunno how far that has gone and if it's still followed. I think ArrayObject is more a behavioural thing than actually replacing the native array in PHP.

Related question: PHP Array and ArrayObject

like image 64
hakre Avatar answered Sep 19 '22 00:09

hakre