Content · Programming · PHP

PHP array_splice() Function: Usage and Examples

Understand array_splice() in PHP with syntax, working code examples, expected output, practical use cases, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When writing PHP applications, you may need to remove or replace a segment of an array in place. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.

The array_splice() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.

What Is array_splice() in PHP?

Remove or replace a segment of an array in place.

Its syntax is:

array_splice(array &$array, int $offset, ?int $length = null, mixed $replacement = []): array

Parameters used by this operation:

  • &$array — The collection being examined or updated.
  • $offset — The starting position in the collection.
  • $length — The number of entries to consider, when provided.
  • $replacement — The value supplied for this argument affects the operation.

Both the returned removed items and the modified input matter. Numeric keys are reindexed; string keys in replacements are not preserved.

Example 1: Basic Usage

Start with a small, self-contained input so you can inspect exactly how the operation behaves.

<?php

$steps = ["start", "draft", "review", "done"];
$removed = array_splice($steps, 1, 2);
print_r($removed);
print_r($steps);

Output:

Array
(
    [0] => draft
    [1] => review
)
Array
(
    [0] => start
    [1] => done
)

The output shows the result of the first call. Check the printed keys and values as well as their order.

Example 2: Another Important Case

This variation highlights another input shape, a comparison option, or a change in the collection.

<?php

$menu = ["Home", "Blog", "Contact"];
array_splice($menu, 1, 1, ["Guides", "Tools"]);
print_r($menu);

Output:

Array
(
    [0] => Home
    [1] => Guides
    [2] => Tools
    [3] => Contact
)

Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.

Practical Example: Update a navigation list by replacing retired entries with new choices

Update a navigation list by replacing retired entries with new choices. Save the code in a PHP file and adapt the sample data to your project.

<?php

$navigation = ["Home", "Old Help", "Contact"];
$removedPages = array_splice($navigation, 1, 1, ["Guides", "FAQ"]);
echo "Removed: " . implode(", ", $removedPages) . PHP_EOL;
echo "Menu: " . implode(" | ", $navigation);

Output:

Removed: Old Help
Menu: Home | Guides | FAQ | Contact

The output comes from the completed workflow shown above. When adapting it, change the source records and keep the same checks for missing, unexpected, or invalid values.

Important Behavior and Edge Cases

Both the returned removed items and the modified input matter. Numeric keys are reindexed; string keys in replacements are not preserved.

  • Test the operation with empty or missing input and decide how your application should respond.
  • Read the return value independently from any changes to variables passed by reference.
  • Validate untrusted input before using the result for permissions, database changes, or user-visible output.

Comparison With Related Functions

Function or ApproachWhen to Use It
array_splice()Remove or replace a segment of an array in place
Related functionsarray_slice() extracts without mutation; array_shift() removes only the first entry.

Common Beginner Mistakes

MistakeCorrect Approach
Assuming input and result have identical keysCheck whether the function reindexes keys or retains original associations.
Confusing mutation with a returned copyBoth the returned removed items and the modified input matter. Numeric keys are reindexed; string keys in replacements are not preserved.
Choosing a related function with different behaviorarray_slice() extracts without mutation; array_shift() removes only the first entry.

Key Takeaways

  • array_splice() helps you remove or replace a segment of an array in place.
  • Both the returned removed items and the modified input matter. Numeric keys are reindexed; string keys in replacements are not preserved.
  • array_slice() extracts without mutation; array_shift() removes only the first entry.
  • Use the practical example as a starting point, but validate real application inputs.

array_splice() is useful when you need to remove or replace a segment of an array in place. Use its documented rules and the examples above to choose the right behavior for your data.

Share this resourceSend this page to someone who may find it useful.