Home > Article > Backend Development > How to delete elements from array in PHP
In PHP programming, array is a very important data structure. The size of the array is variable, and elements can be continuously added to it. However, sometimes it is necessary to delete elements from an array to ensure the data integrity of the array.
This article will introduce how to delete elements in an array in PHP. We'll explore methods of removing single elements and multiple elements, and discuss the pros and cons of each method.
1. Delete a single element
In PHP, you can use the unset function to delete a single element in an array. The unset function is a special function in PHP used to delete variables. It deletes one or more variables and frees the memory space they occupy.
The following is a sample code that uses the unset function to delete a single element:
<?php $numbers = array(1, 2, 3, 4, 5); unset($numbers[2]); print_r($numbers); ?>
Run the above code, the output is as follows:
Array ( [0] => 1 [1] => 2 [3] => 4 [4] => 5 )
In the code, we define an array $numbers , which contains 5 elements. Then, we use the unset function to delete the third element in the $numbers array (the element with index 2). Finally, we use the print_r function to output the $numbers array after removing elements.
It should be noted that when using the unset function to delete an element in an array, the subscript of the element will no longer exist. In the above example, we have deleted the element with index 2, so the elements in the $numbers array have indexes 0, 1, 3, 4.
2. Delete multiple elements
In PHP, to delete multiple array elements at the same time, you can use the array filter (array_filter) function. The array filter function is used to iterate over the elements in the array, execute a callback function, and then use the return value of the callback function as the elements of the new array. Elements whose callback function returns false will be deleted.
The following is a sample code that uses the array filter function to delete multiple elements:
<?php $numbers = array(1, 2, 3, 4, 5); $indexes = array(1, 3); $filtered = array_filter($numbers, function($key) use ($indexes) { return !in_array($key, $indexes); }, ARRAY_FILTER_USE_KEY); print_r($filtered); ?>
Run the above code, the output is as follows:
Array ( [0] => 1 [2] => 3 [4] => 5 )
In the code, we define a The array $numbers, which contains 5 elements, also defines an array $indexes, which contains the indexes of the elements to be deleted. Then, we use the array filter function to traverse the $numbers array, filter elements that meet the conditions, and finally return a new array.
Using the array filter function to delete multiple elements can be more flexible and decide which elements to delete based on a variety of conditions.
Summary:
The above is the detailed content of How to delete elements from array in PHP. For more information, please follow other related articles on the PHP Chinese website!