Home > Article > Backend Development > How to delete the array value of the specified subscript in php
php method to delete the specified subscript array value: 1. Use the unset() function, the syntax "unset($arr[specified subscript]);"; 2. Use the array_splice() function, the syntax "array_splice ($arr, specified subscript, 1)".
The operating environment of this tutorial: Windows 7 system, PHP version 7.1, DELL G3 computer
PHP delete specified Target array value
Method 1: Use the unset() function
If you want to delete an element in the array, you can use unset( ).
The unset() function allows you to cancel elements in an array, but the array will not re-index, that is, the original index will be maintained, because the index in PHP has a special meaning.
Example: Delete the element with index 2
<?php $arr = array(1 => 'one', 2 => 'two', 3 => 'three'); echo '<pre class="brush:php;toolbar:false">'; //删除下标为2的元素 unset($arr[2]); print_r($arr); ?>
Output result:
Array ( [1] => one [3] => three )
Method 2: Use array_splice() function
array_splice() function can be used to delete array elements of a specified length starting from a specified position.
You only need to set the second parameter of the function to the specified subscript, and set the third parameter to 1 (delete an element).
<?php header("Content-type:text/html;charset=utf-8"); $arr = array('one','two','three','php'); echo '<pre class="brush:php;toolbar:false">'; //删除下标为2的元素 array_splice($arr,2,1); print_r($arr); ?>
Output results:
Array ( [0] => one [1] => two [2] => php )
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to delete the array value of the specified subscript in php. For more information, please follow other related articles on the PHP Chinese website!