Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

array_splice()

Replace a positional segment in the original array and return the removed elements.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

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

Replace a positional segment in the original array and return the removed elements. The replacement can have a different length, so later items move. Numeric keys are reindexed and replacement keys are ignored. A zero length inserts new items without removing existing ones.

Signature

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

Example output

[["B"],["A","X","Y","C"]]

Examples

<?php
$queue = ['A','B','C'];
$removed = array_splice($queue, 1, 1, ['X','Y']);
echo json_encode([$removed, $queue]);

Result

[["B"],["A","X","Y","C"]]

Sources