Home > Backend Development > PHP Problem > How to delete specified elements of an array in php

How to delete specified elements of an array in php

PHPz
Release: 2023-04-27 16:25:42
Original
1078 people have browsed it

In PHP, array is a very common data type that can store multiple associated values ​​and also provides many convenient methods to manipulate these values. In actual development, we often need to perform some specific operations on the array, such as deleting specified elements. PHP provides a variety of methods to implement this operation. Let’s introduce them one by one below.

1. Use the unset function

The simplest way to delete a specified element in an array is to use the unset function, which can be used to destroy the specified variable. For example, we have an array $fruit, which contains three elements: apples, bananas and oranges. Now if we want to delete the bananas, we can do this:

$fruit = array("苹果", "香蕉", "橘子");

unset($fruit[1]);
Copy after login

At this time, only the $fruit array remains It contains two elements: apple and orange. It should be noted that the unset function can only be used to delete elements in an array. If you want to delete the entire array, you need to use the unset($array) form.

2. Use the array_splice function

The array_splice function can insert or delete elements in the array and return the deleted element. Its syntax is as follows:

array_splice($array, $offset, $length, $replacement);
Copy after login

Among them, $array is the array to be operated on, $offset is the array subscript to be specified, $length is the number of elements to be deleted, and $replacement is the element to be inserted (can is a single element or an array).

For example, we have an array $number, which contains 5 elements: 1, 2, 3, 4 and 5. Now if we want to delete 2 and 3, we can do this:

$number = array(1, 2, 3, 4, 5);

array_splice($number, 1, 2);
Copy after login

At this time, there are only three elements left in the $number array: 1, 4 and 5.

It should be noted that if one or more elements are to be inserted, the $length parameter must be set to 0, otherwise the original elements will be replaced.

3. Use the array_diff function

The array_diff function is used to compare the difference set of two or more arrays and return the difference set result. If we want to delete specified elements in an array, we can take the difference between the original array and the array containing these elements, and the result is the deleted array. For example:

$array1 = array("苹果", "香蕉", "橘子");

$array2 = array("香蕉");

$result = array_diff($array1, $array2);
Copy after login

At this time, $result is the array after deleting the bananas, that is, the array containing apples and oranges.

It should be noted that this method only applies to deleting a single element in an array. If you want to delete multiple elements, you need to put them all in an array and calculate the difference.

4. Use the array_filter function

The array_filter function can filter the elements in the array according to the specified conditions and return the filtered results. If we want to delete elements in an array that meet certain conditions, we can use the array_filter function to achieve this. For example:

$fruit = array("苹果", "香蕉", "橘子");

$fruit = array_filter($fruit, function($value){
    return $value != "香蕉";
});
Copy after login

At this time, there are only two elements left in the $fruit array: apples and oranges.

It should be noted that the array_filter function does not directly delete elements in the array, but returns elements that meet the conditions according to the specified conditions. Therefore, the returned result needs to be reassigned to the original array to achieve the deletion effect.

Summary:

The above are several methods of deleting specified elements in an array in PHP. They each have their own advantages and disadvantages. The specific use needs to be selected according to the actual situation. It should be noted that when deleting array elements, you must ensure that the order of other elements in the array is not affected, otherwise unpredictable errors may occur.

The above is the detailed content of How to delete specified elements of 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template