array_splice
Description
The array_splice of Array for PHP removes a portion of the array and replace it with something else.
Syntax
array_splice( array &$array, int $offset, ?int $length = null, mixed $replacement = [] ): array
Parameters
array
The input array.
offset
If offset is positive, then the start of the removed portion is at that offset from the beginning of the array.
If offset is negative, then the start of the removed portion is at that offset from the end of the array.
length
If length is omitted, everything is removed from offset to the end of the array.
If length is positive, then that many elements will be removed.
If length is negative, then the end of the removed portion will be that many elements from the end of the array.
If length is zero, no elements will be removed.
replacement
If replacement array is specified, then the removed elements are replaced with elements from this array.
If offset and length are such that nothing is removed, then the elements from the replacement array are inserted in the place specified by the offset.
If replacement is just one element it is not necessary to put array() or square brackets around it, unless the element is an array itself, an object or null.
NOTE: Keys in the replacement array are not preserved.
Return
Returns an array consisting of the extracted elements.
Examples
1 · array offset · negative
<? $array = [ "a", "b", "c", "d", "e", "f" ]; $offset = -2; $return = array_splice($array, $offset); print_r($return);
Array ( [0] => e [1] => f )
2 · array offset · zero
<? $array = [ "a", "b", "c", "d", "e", "f" ]; $offset = 0; $return = array_splice($array, $offset); print_r($return);
Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f )
3 · array offset · positive
<? $array = [ "a", "b", "c", "d", "e", "f" ]; $offset = 2; $return = array_splice($array, $offset); print_r($return);
Array ( [0] => c [1] => d [2] => e [3] => f )
4 · length · negative
<? $array = [ "a", "b", "c", "d", "e", "f" ]; $offset = 0; $length = -2; $return = array_splice($array, $offset, $length); print_r($return);
Array ( [0] => a [1] => b [2] => c [3] => d )
5 · length · zero
<? $array = [ "a", "b", "c", "d", "e", "f" ]; $offset = 0; $length = 0; $return = array_splice($array, $offset, $length); print_r($return);
Array ( )
6 · length · positive
<? $array = [ "a", "b", "c", "d", "e", "f" ]; $offset = 0; $length = 2; $return = array_splice($array, $offset, $length); print_r($return);
Array ( [0] => a [1] => b )
7 · length > array
<? $array = [ "a", "b", "c", "d", "e", "f" ]; $offset = 0; $length = 100; $return = array_splice($array, $offset, $length); print_r($return);
Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f )
8 · replacement
<? $array = [ "a", "b", "c", "d", "e", "f" ]; $offset = 1; $length = 1; $replacement = [ 0, 1, 2 ]; $return = array_splice($array, $offset, $length, $replacement); print_r($return); print_r($array);
Array ( [0] => b ) Array ( [0] => a [1] => 0 [2] => 1 [3] => 2 [4] => c [5] => d [6] => e [7] => f )
Links
Related
Array
- array
- array_all
- array_any
- array_change_key_case
- array_chunk
- array_column
- array_combine
- array_count_values
- array_diff
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_fill
- array_fill_keys
- array_filter
- array_find
- array_find_key
- array_flip
- array_intersect
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_key_exists
- array_key_first
- array_key_last
- array_keys
- array_map
- array_merge
- array_merge_recursive
- array_multisort
- array_pad
- array_pop
- array_product
- array_push
- array_rand
- array_reduce
- array_replace
- array_replace_recursive
- array_reverse
- array_search
- array_shift
- array_slice
- array_sum
- array_udiff
- array_udiff_assoc
- array_udiff_uassoc
- array_uintersect
- array_uintersect_assoc
- array_uintersect_uassoc
- array_unique
- array_unshift
- array_values
- array_walk
- array_walk_recursive
- arsort
- asort
- compact
- count
- current
- end
- extract
- in_array
- key
- key_exists
- krsort
- ksort
- list
- natcasesort
- natsort
- next
- pos
- prev
- range
- reset
- rsort
- shuffle
- sizeof
- sort
- uasort
- uksort
- usort