Reputation: 13
I'm removing a set of elements with array slice, from e certain offset to the end.
How can I get the elements that were removed, in a different array?
Upvotes: 1
Views: 2278
Reputation: 141827
You just need to use array_slice
twice:
$begin = array_slice($array, 0, 5);
$end = array_slice($array, 5);
Now $begin
contains the first 5 elements of $array
, and $end
contains the rest.
Upvotes: 4
Reputation: 7953
array_slice
will return an array containing the elements that were removed. You can also use array_diff
to find the elements that were not removed.
$original = array('1','2','3','4','5');
$sliced = array_slice($original,1); // 2, 3, 4, 5
$diff = array_diff($original,$sliced); // 1
Upvotes: 0