PHP is a popular programming language that provides a variety of powerful functions to manipulate data. In PHP, an array is a special data type that can store multiple values, and these values can be of any data type.
Sometimes, we need to delete one or more elements from an array in order to modify the data directly. This article will explain the different ways on how to delete array elements using PHP.
In PHP, you can use the unset() function to delete elements in an array. This function accepts one or more parameters, each parameter is the key name of the array element to be deleted.
For example, in the following example, we create an array $a, which contains 5 elements. Then we delete the second and fourth elements in the array through the unset() function.
$a = array('a', 'b', 'c', 'd', 'e'); unset($a[1], $a[3]); print_r($a);
The above code will output the following:
Array ( [0] => a [2] => c [3] => e )
As shown above, the second and fourth elements of the array have been removed.
Another way to remove elements from a PHP array is to use the array_splice() function. This function deletes one or more array elements and returns an array of deleted elements.
$a = array('a', 'b', 'c', 'd', 'e'); $removed = array_splice($a, 1, 2); print_r($a); print_r($removed);
In the above example, we have removed the second and third elements from the array $a using the array_splice() function and stored them in the $removed array.
The output results are as follows:
Array ( [0] => a [1] => d [2] => e ) Array ( [0] => b [1] => c )
The above code outputs the array $a after the deleted elements and the $removed array composed of the deleted elements.
In the above example, the second parameter 1 represents the starting position of the element to be deleted, and the third parameter 2 represents the number of elements to be deleted.
array_filter() function can also be used to delete elements in an array. The first parameter of this function is the array to be filtered, and the second parameter is a callback function. The callback function will be executed on each array element and return a boolean value.
If the callback function returns true, the element is retained. Otherwise, delete the element.
Here is an example of using array_filter() function to remove elements less than 5 from array $a:
$a = array(1, 6, 2, 7, 3, 8, 4, 9, 5); $a = array_filter($a, function($value) { return $value >= 5; }); print_r($a);
In the above example, the callback function returns true only if the element value is greater than or equal to 5 o'clock. The array_filter() function will remove all elements less than 5 from array $a and return the filtered array.
The output result is as follows:
Array ( [1] => 6 [3] => 7 [5] => 8 [7] => 9 )
In the above code, the result array only retains elements greater than or equal to 5.
The last method is to use the unset() function to delete the array elements, and then reset them through the array_values() function Index array. This will increment the keys of elements after removing them. This method can also remove holes in numeric key names.
For example, in the following example, we use the unset() function to delete the second element in the array $a and use the array_values() function to re-index the array:
$a = array('a', 'b', 'c', 'd', 'e'); unset($a[1]); $a = array_values($a); print_r($a);
In the above In the example, we delete the second element in the array (i.e. $b) and then re-index the array $a using the array_values() function.
The output result is as follows:
Array ( [0] => a [1] => c [2] => d [3] => e )
The second element in the array was successfully deleted, and its key name became 2.
No matter which method you choose, PHP provides many ways to remove elements from an array. Which method to use depends on your needs and programming style.
The above is the detailed content of php array deletion can. For more information, please follow other related articles on the PHP Chinese website!