Remove a portion of the array and optionally replace it with something else

array:remove_slice(ARRAY $array, INT $offset, INT $length=NULL, $replacement=[ ]) : ARRAY

# remove the first element -- start from index 0 and remove 1 element
array:remove_slice(["red", "green", "blue", "yellow", "black"], 0, 1)
#=> [ "green", "blue", "yellow", "black" ]

# remove first two elements -- start from index 0 and remove 2 elements
array:remove_slice(["red", "green", "blue", "yellow", "black"], 0, 2)
#=> [ "blue", "yellow", "black" ]

# remove index 2 to 5 -- start from index 2 and remove 3 elements
array:remove_slice(["red", "green", "blue", "yellow", "black"], 2, 3)
#=> [ "red", "green" ]

# remove all elements except the first two -- start from index 2 and remove the rest
array:remove_slice(["red", "green", "blue", "yellow", "black"], 2)
#=> [ "red", "green" ]

# remove last two elements -- start from index -2 (from end) and remove the rest
array:remove_slice(["red", "green", "blue", "yellow", "black"], -2)
#=> [ "red", "green", "blue" ]

# insert new item in an array at offset position
array:remove_slice([1,2,3,4,5], 2, 0, 11)
#=> [
#=>     1,
#=>     2,
#=>     11,
#=>     3,
#=>     4,
#=>     5
#=> ]

#===
array:remove_slice([1 => 1, 2 => 2,3 => 3,3 => 4,5 => 5], 2, 0, 11)
#=> [
#=>     1,
#=>     2,
#=>     11,
#=>     4,
#=>     5
#=> ]

Copyright ©2013-2022 SunSed®. All rights reserved.