How to delete specified elements from an array in php

PHPz
Release: 2023-04-25 09:28:58
Original
569 people have browsed it

In PHP development, we often encounter situations where we need to delete a specified element from an array. This is a very common operation. Removal of different elements is a basic operation and many methods are provided in PHP to achieve it. In this article, we'll cover some common methods, along with their pros and cons.

  1. Use the unset function

The most common method is to use the unset function, which is used to destroy the value of the specified variable. In this case, we can use it to destroy the value of the specified element of the array. The code is as follows:

$fruits = array('apple', 'banana', 'orange', 'kiwi', 'peach'); unset($fruits[2]); //删除下标为2的元素 print_r($fruits); //输出结果为:Array ( [0] => apple [1] => banana [3] => kiwi [4] => peach )
Copy after login

In the above code, we delete the element with subscript 2 in the array, which is the "orange" element in the array, through the unset function.

The advantage of this method is that it is simple and convenient, but the disadvantage is that it does not rearrange the index of the array elements, that is, the index position of the deleted element will always exist, and if the array is referenced in other parts of the program , problems will occur at this time, so you need to pay attention.

  1. Use the array_splice function

The array_splice function can insert or delete one or more elements in an array. Use this function to delete one or more elements and rebuild the array to fill the gaps. The code is as follows:

$fruits = array('apple', 'banana', 'orange', 'kiwi', 'peach'); array_splice($fruits, 2, 1); print_r($fruits); //输出结果为:Array ( [0] => apple [1] => banana [2] => kiwi [3] => peach )
Copy after login

In the above code, we use the array_splice function to delete the element with subscript 2 in the array, which is the "orange" element in the array. This method will rearrange the index of the array elements and return the deleted element array, which makes programming more convenient.

  1. Using the array_diff function

The array_diff function returns an array that contains elements that are in one array but not in any other array. We can use this function to delete specified elements. The code is as follows:

$fruits = array('apple', 'banana', 'orange', 'kiwi', 'peach'); $fruits = array_diff($fruits, ['orange']); print_r($fruits); //输出结果为:Array ( [0] => apple [1] => banana [3] => kiwi [4] => peach )
Copy after login

In the above code, we use the array_diff function to return a new array that does not contain "orange". We overwrite the original array with this new array.

  1. Using unset and array_values functions

We can first use the unset function to delete the specified element, and then use the array_values function to reset the index of the array, so that the specified element can be deleted And the purpose of rearranging the index. The code is as follows:

$fruits = array('apple', 'banana', 'orange', 'kiwi', 'peach'); unset($fruits[2]); $fruits = array_values($fruits); print_r($fruits); //输出结果为:Array ( [0] => apple [1] => banana [2] => kiwi [3] => peach )
Copy after login

In the above code, we use the unset function to delete the element with index 2 in the array, and then use the array_values function to reset the index of the array. This approach also works for string-indexed associative arrays.

Summary

In PHP, deleting specified elements in an array is a very common operation. We introduced four different methods: using the unset function, using the array_splice function, using the array_diff function, and using the unset and array_values functions. Each of these methods has its advantages and disadvantages, and the most appropriate method should be selected based on the specific situation.

I hope readers can learn about the advantages and disadvantages of various methods from this article and improve their PHP development skills.

The above is the detailed content of How to delete specified elements from an array in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!