The usage of array deletion in php is: [array_splice()] function is used to delete part of the elements of the array. The syntax is [array array_splice (array &$arr, int $start [, int $length = 0.. 】.
The usage of array deletion in PHP is:
PHP array_splice() function is used to delete part of the array Element; you can delete it directly or replace it with other values.
array_splice() The syntax is as follows:
array array_splice ( array &$arr, int $start [, int $length = 0 [, mixed $replacement ]] )
Parameter description:
1. arr represents an array.
2. start indicates the position (subscript) where deletion begins:
If start is a positive number, delete from front to back.
If start is a negative number, start from the position -start from the end of arr and delete from back to front. For example, -2 means starting from the penultimate element of the array.
3. Length is an optional parameter, indicating the number of elements to be deleted:
If length is a positive number, it means length elements are deleted;
If length is a negative number, then all elements starting from start and counting down to length from the end of the array will be deleted;
If it is omitted, then all elements starting from start will be deleted , all the elements to the end of the array.
4. Replacement is an optional parameter, indicating the value to be replaced. If replacement has multiple values, it needs to be set to an array. If there is only one The value does not need to be set to an array.
If the result of the combination of start and length is that no elements will be deleted, then the value contained in replacement will be inserted into the position specified by start.
Note that using replacement replaces array elements without retaining the original key names.
Return value: Returns an array composed of deleted elements.
Examples of using the function are as follows:
<?php $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 2); print_r($arr); //$arr 现在变成 array("red", "green") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 1, -1); print_r($arr); //$arr 现在变成 array("red", "yellow") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 1, count($arr), "orange"); print_r($arr); //$arr 现在变成 array("red", "orange") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, -1, 1, array("black", "maroon")); print_r($arr); //$input 现在变成 array("red", "green", "blue", "black", "maroon") $arr = array("red", "green", "blue", "yellow"); array_splice($arr, 3, 0, "purple"); print_r($arr); //$arr 现在变成 array("red", "green", "blue", "purple", "yellow"); ?>
The output result of executing the above program is as follows:
Array ( [0] => red [1] => green ) Array ( [0] => red [1] => yellow ) Array ( [0] => red [1] => orange ) Array ( [0] => red [1] => green [2] => blue [3] => black [4] => maroon ) Array ( [0] => red [1] => green [2] => blue [3] => purple [4] => yellow )
Related free learning recommendations: php programming (video)
The above is the detailed content of What is the usage of array deletion in php. For more information, please follow other related articles on the PHP Chinese website!