array_splice

(PHP 4, PHP 5, PHP 7, PHP 8)

array_spliceRemove a portion of the array and replace it with something else

Description

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

Removes the elements designated by offset and length from the array array, and replaces them with the elements of the replacement array, if supplied.

Note:

Numerical keys in array are not preserved.

Note: If replacement is not an array, it will be typecast to one (i.e. (array) $replacement). This may result in unexpected behavior when using an object or null replacement.

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 array.

If offset is negative then the start of the removed portion is at that offset from the end of the array array.

length

If length is omitted, removes everything from offset to the end of the array.

If length is specified and is positive, then that many elements will be removed.

If length is specified and is negative, then the end of the removed portion will be that many elements from the end of the array.

If length is specified and is zero, no elements will be removed.

Tip

To remove everything from offset to the end of the array when replacement is also specified, use count($input) for length.

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.

Note:

Keys in the replacement array are not preserved.

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.

Return Values

Returns an array consisting of the extracted elements.

Changelog

Version Description
8.0.0 length is nullable now.

Examples

Example #1 array_splice() examples

<?php
$input 
= array("red""green""blue""yellow");
array_splice($input2);
var_dump($input);

$input = array("red""green""blue""yellow");
array_splice($input1, -1);
var_dump($input);

$input = array("red""green""blue""yellow");
array_splice($input1count($input), "orange");
var_dump($input);

$input = array("red""green""blue""yellow");
array_splice($input, -11, array("black""maroon"));
var_dump($input);
?>

The above example will output:

array(2) {
  [0]=>
  string(3) "red"
  [1]=>
  string(5) "green"
}
array(2) {
  [0]=>
  string(3) "red"
  [1]=>
  string(6) "yellow"
}
array(2) {
  [0]=>
  string(3) "red"
  [1]=>
  string(6) "orange"
}
array(5) {
  [0]=>
  string(3) "red"
  [1]=>
  string(5) "green"
  [2]=>
  string(4) "blue"
  [3]=>
  string(5) "black"
  [4]=>
  string(6) "maroon"
}

Example #2 Equivalent statements to various array_splice() examples

The following statements are equivalent:

<?php

// append two elements to $input
array_push($input$x$y);
array_splice($inputcount($input), 0, array($x$y));

// remove the last element of $input
array_pop($input);
array_splice($input, -1);

// remove the first element of $input
array_shift($input);
array_splice($input01);

// insert an element at the start of $input
array_unshift($input$x$y);
array_splice($input00, array($x$y));

// replace the value in $input at index $x
$input[$x] = $y// for arrays where key equals offset
array_splice($input$x1$y);

?>

See Also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial