Home  >  Article  >  Backend Development  >  How to remove three elements from an array in php

How to remove three elements from an array in php

PHPz
PHPzOriginal
2023-04-20 09:08:15783browse

When writing PHP code, sometimes you need to process elements in an array. One situation you may encounter is when you need to remove several elements from an array. There are many ways to delete one or several elements, and we will cover some of them.

First, we need to define an array containing some elements. In this article, we will use the following example array:

$animals = array('dog', 'cat', 'bird', 'fish', 'lion', 'tiger', 'elephant', 'monkey');

This array contains 8 animal names. In this article, we will demonstrate how to delete three of the elements.

Method 1: Use the unset() function to delete elements

The most basic way to delete array elements is to use PHP’s built-in unset() function. This function will delete the element with the specified key in the array. The following is a sample code to remove three elements from an array using the unset() function:

unset($animals[2]); //删除数组中索引为2的元素,也就是'bird'
unset($animals[4]); //删除数组中索引为4的元素,也就是'lion'
unset($animals[6]); //删除数组中索引为6的元素,也就是'elephant'

After calling the unset() function, the element will be completely removed from the array and the index values ​​of other elements will be updated. As in the above code, after calling unset($animals[2]), the element 'bird' at index 2 of the array is deleted, and the index values ​​of other elements are updated, so $array[3] is now 'fish'.

Method 2: Use the array_splice() function to delete elements

Another way to delete elements from a PHP array is to use the built-in array_splice() function. This function removes a specified element from an array and moves the index values ​​of the remaining elements to maintain continuity. The following is sample code to remove three elements from an array using the array_splice() function:

array_splice($animals, 2, 1); //'bird'从索引2出删除1个元素
array_splice($animals, 4, 1); //'lion'从索引4出删除1个元素
array_splice($animals, 6, 1); //'elephant'从索引6出删除1个元素

The first parameter of the array is the array from which the elements need to be removed. The second parameter is the actual position of the element to be deleted. The third parameter is the number of elements to be deleted. Therefore, after array_splice($animals,2,1) is executed, the 'bird' at index 2 of the array is deleted, and the indexes of other elements of the array will be moved to maintain continuity.

Method 3: Use array_diff() function

The last method is to use PHP’s built-in array_diff() function and array_merge() function. The following is a sample code that uses the array_diff() function to delete three elements from an array:

$to_delete = array('bird', 'lion', 'elephant');//需要删除的元素放到一个新数组中
$animals = array_merge(array_diff($animals, $to_delete));//使用array_diff()筛选出不需要删除的元素,再使用array_merge()合并数组

The process of this method looks more complicated, but in fact it just creates a new array $to_delete with the elements that need to be deleted. , and use the array_diff() function to filter out elements that do not need to be deleted. Finally, use the array_merge() function to merge the arrays after the delete operation.

Summary

PHP There are many ways to extract specified elements from an array. We have introduced three of them: using the unset() function, using the array_splice() function and using the array_diff() function. . We need to choose different methods according to the specific situation.

If we only need to delete one or a few elements in the array, we can use the unset() function or array_splice() function.

If we need to delete multiple elements or delete elements according to specific conditions, we can use the array_filter() function or a combination of the array_diff() function and the array_merge() function.

The above is the detailed content of How to remove three elements from an array 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