array_splice() / array_slice()
| Since: | PHP 4(2000) |
|---|
Removes, inserts, or replaces elements at any position in an array, or extracts a portion of an array.
Syntax
array_splice(array, offset, length, replacement); // Extracts a portion of an array and returns it as a new array array_slice(array, offset, length, preserve_keys);
Function List
| Function | Description |
|---|---|
| array_splice($array, $offset, $length, $replacement) | Removes elements from the specified position in an array and optionally inserts new elements in their place. Returns the removed elements as an array. The original array is modified. |
| array_slice($array, $offset, $length, $preserve_keys) | Extracts a specified number of elements starting at the given position and returns them as a new array. The original array is not modified. |
Sample Code
sample_array_splice.php
<?php $members = ['Okabe Rintaro', 'Makise Kurisu', 'Shiina Mayuri', 'Hashida Itaru', 'Amane Suzuha']; // Extracts 2 elements starting at index 1 (original array is unchanged) $sliced = array_slice($members, 1, 2); print_r($sliced); // Uses a negative offset to extract from the end $last_two = array_slice($members, -2); print_r($last_two); // Extracts elements while preserving the original keys $preserved = array_slice($members, 2, 2, true); print_r($preserved); // Removes 2 elements starting at index 1 and inserts new elements $items = ['A', 'B', 'C', 'D', 'E']; $removed = array_splice($items, 1, 2, ['X', 'Y', 'Z']); print_r($removed); print_r($items); // Inserts elements without removing any $list = ['Mon', 'Wed', 'Fri']; array_splice($list, 1, 0, ['Tue']); print_r($list);
array_splice.php
php array_splice.php
Array
(
[0] => Makise Kurisu
[1] => Shiina Mayuri
)
Array
(
[0] => Hashida Itaru
[1] => Amane Suzuha
)
Array
(
[2] => Shiina Mayuri
[3] => Hashida Itaru
)
Array
(
[0] => B
[1] => C
)
Array
(
[0] => A
[1] => X
[2] => Y
[3] => Z
[4] => D
[5] => E
)
Array
(
[0] => Mon
[1] => Tue
[2] => Wed
[3] => Fri
)
Notes
array_splice() is a destructive function that modifies the original array directly. It can delete, insert, and replace elements in a single call. Passing 0 as the third argument inserts without removing; omitting the fourth argument removes without inserting.
array_slice() is a non-destructive function that does not modify the original array. Passing true as the fourth argument preserves the original keys. By default, numeric keys are reindexed starting from 0, specify true when key values matter.
If you only need to add or remove elements at the beginning or end of an array, array_push() / array_pop() / array_shift() / array_unshift() offer a simpler approach.
array_splice modifies the original array
array_splice() directly modifies the original array (destructive). To preserve the original array, use array_slice() instead.
<?php $members = ['Okabe Rintaro', 'Makise Kurisu', 'Shiina Mayuri', 'Hashida Itaru', 'Amane Suzuha']; // array_splice: modifies the original array $copy1 = $members; array_splice($copy1, 1, 2); print_r($copy1); // 2 elements removed from the copy // array_slice: does not modify the original array $copy2 = $members; $sliced = array_slice($copy2, 1, 2); print_r($copy2); // unchanged print_r($sliced); // extracted portion returned as a new array
splice_destructive.php
php splice_destructive.php
Array
(
[0] => Okabe Rintaro
[1] => Hashida Itaru
[2] => Amane Suzuha
)
Array
(
[0] => Okabe Rintaro
[1] => Makise Kurisu
[2] => Shiina Mayuri
[3] => Hashida Itaru
[4] => Amane Suzuha
)
Array
(
[0] => Makise Kurisu
[1] => Shiina Mayuri
)
A negative offset counts from the end
An offset of -1 points just before the last element. array_slice() behaves the same way.
<?php $members = ['Okabe Rintaro', 'Makise Kurisu', 'Shiina Mayuri', 'Hashida Itaru', 'Amane Suzuha']; // offset=-1 means before the last element (Amane Suzuha) $last = array_slice($members, -1); print_r($last); // ['Amane Suzuha'] // offset=-2 means before the second-to-last element $last_two = array_slice($members, -2); print_r($last_two); // ['Hashida Itaru', 'Amane Suzuha']
splice_negative.php
php splice_negative.php
Array
(
[0] => Amane Suzuha
)
Array
(
[0] => Hashida Itaru
[1] => Amane Suzuha
)
Passing 0 as the third argument inserts without removing
A length of 0 inserts elements without deleting any. Passing null removes everything from the offset to the end — do not confuse the two.
<?php $members = ['Okabe Rintaro', 'Shiina Mayuri', 'Hashida Itaru']; // Third argument is 0: insert without removing array_splice($members, 1, 0, ['Makise Kurisu']); print_r($members); // Makise Kurisu inserted after Okabe // Third argument is null: remove everything from offset to end $members2 = ['Okabe Rintaro', 'Shiina Mayuri', 'Hashida Itaru']; array_splice($members2, 1, null); print_r($members2); // Only ['Okabe Rintaro'] remains
splice_insert.php
php splice_insert.php
Array
(
[0] => Okabe Rintaro
[1] => Makise Kurisu
[2] => Shiina Mayuri
[3] => Hashida Itaru
)
Array
(
[0] => Okabe Rintaro
)
Practical Patterns
Moving an element (reordering a list)
Using array_splice() to move an element to a different position.
<?php $members = ['Okabe Rintaro', 'Makise Kurisu', 'Shiina Mayuri', 'Hashida Itaru', 'Amane Suzuha']; // Move Shiina Mayuri (index 2) to the front $moved = array_splice($members, 2, 1); // extract array_splice($members, 0, 0, $moved); // insert at front print_r($members);
splice_move.php
php splice_move.php
Array
(
[0] => Shiina Mayuri
[1] => Okabe Rintaro
[2] => Makise Kurisu
[3] => Hashida Itaru
[4] => Amane Suzuha
)
Pagination (extracting a slice of an array)
Using array_slice() to split an array into pages.
<?php $all_members = ['Okabe Rintaro', 'Makise Kurisu', 'Shiina Mayuri', 'Hashida Itaru', 'Amane Suzuha']; $page = 2; $per_page = 2; $offset = ($page - 1) * $per_page; $current_page = array_slice($all_members, $offset, $per_page); echo 'Page ' . $page . ':'; print_r($current_page);
splice_page.php
php splice_page.php
Page 2:Array
(
[0] => Shiina Mayuri
[1] => Hashida Itaru
)
If you find any errors or copyright issues, please contact us.