Home  >  Article  >  Backend Development  >  How to use array_splice function in php

How to use array_splice function in php

青灯夜游
青灯夜游Original
2019-05-24 17:24:403731browse

Usage of array_splice function in php: [array_splice(array,start,length,array)]. The array_splice() function is used to remove the specified element from an array, replace it with a new element, and return the array with the specified element removed.

How to use array_splice function in php

array_splice() is a built-in function of PHP, used to remove specified elements from an array and replace them with new elements; you can also add other elements to the array element. This function typically replaces existing elements with elements from other arrays and returns an array with the deleted or replaced elements.

(Recommended tutorial: php video tutorial)

How to use the php array_splice() function?

array_splice() function removes the selected element from an array and replaces it with a new element. This function typically replaces existing elements with elements from other arrays and returns an array with the deleted or replaced elements. The

array_splice() function is an advanced and extended version of the array_slice() function. Use this function not only to remove elements from an array, but also to add other elements to the array.

Basic syntax:

array_splice(array,start,length,array)

Parameters: This function can take four parameters

●array: required. Specifies an array.

● start: required. numerical value. Specifies the starting position of deleted elements. 0 = first element. If the value is set to a positive number, removal begins at the offset in the array specified by the value. If the value is set to a negative number, removal begins at the offset specified by the value from the end of the array. -2 means start from the second to last element of the array.

●length: optional. numerical value. Specifies the number of elements to be removed, which is also the length of the returned array. If the value is set to a positive number, that number of elements are removed. If this value is set to a negative number, all elements from start to length inverse of the end of the array are removed. If this value is not set, all elements from the position set by the start parameter to the end of the array are removed.

●array: optional. Specifies the array with the elements to be inserted into the original array. If there is only one element, it can be set to a string and does not need to be set to an array.

Return value: Returns the array containing the extracted elements.

Note: If the function does not remove any elements (length=0), the replacement array will be inserted from the position of the start parameter.

Let’s take a look at how to use the php array_splice() function through an example.

Example 1:

<?php
$a1=array("a"=>"PHP","b"=>"JAVA","c"=>"MYSQL","d"=>"LINUX");
$a2=array("PHP","MYSQL");
array_splice($a1,0,2,$a2);
print_r($a1);
?>

Output:

Array ( [0] => PHP [1] => MYSQL [c] => MYSQL [d] => LINUX )

Example 2:

<?php
$a1=array("0"=>"red","1"=>"green");
$a2=array("0"=>"purple","1"=>"orange");
array_splice($a1,1,0,$a2);
print_r($a1);
?>

Output:

Array ( [0] => red [1] => purple [2] => orange [3] => green )

The above is the detailed content of How to use array_splice function in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn