Home  >  Article  >  Backend Development  >  How to delete specified elements from an array using PHP

How to delete specified elements from an array using PHP

PHPz
PHPzOriginal
2023-04-27 09:08:34449browse

PHP is a very popular server-side scripting language used for creating dynamic web pages. When developing in PHP, you often need to operate on arrays. Sometimes we need to delete specified elements in an array. This article will introduce how to use PHP to delete specified elements in an array.

  1. Use the unset() function to delete array elements

There is a function in PHP called unset(), which can be used to destroy specified variables, including elements in the array. Therefore, we can use the unset() function to delete the specified element in the array. The basic syntax is as follows:

unset($array[key]);

Among them, $array represents the array to be operated on, and key represents the subscript of the element to be deleted.

For example, we have an array $fruits, which stores the names of four fruits. The code is as follows:

$fruits = array("apple", "banana", "orange", "grape");

We want to delete the element "banana" with index 1 in the array, You can use the unset() function to operate. The code is as follows:

unset($fruits[1]);

After running the above code, $fruits will become a new array as shown below:

Array ( [0] => apple [2] => orange [3] => grape )

As you can see, The element with index 1 in the array has been deleted.

  1. Use the array_splice() function to delete array elements

In addition to using the unset() function, there is also a function in PHP called array_splice(), which can be used to delete array elements. The element at the specified position and replaces the deleted element with another value. The basic syntax is as follows:

array_splice($array, $offset, $length, $replacement);

Among them, $array represents the array to be operated on, $offset represents the starting position of the element to be deleted, $length represents the number of elements to be deleted, and $replacement represents the replacement. The value of the deleted element. If the value of $length is 0, no elements will be deleted, only insertion operations will be performed.

For example, we still use the array $fruits in the above example. We want to delete the element "banana" with the subscript 1 in the array, and move the elements after the subscript 2 forward one position. You can use the array_splice() function, the code is as follows:

array_splice($fruits, 1, 1);

After running the above code, $fruits will become a new array as shown below:

Array ( [0] => apple [1] => orange [2] => grape )

As you can see, the middle and lower part of the array The element "banana" with index 1 has been deleted, and the elements after index 2 have been moved forward by one position.

  1. Use the array_filter() function to delete array elements

In addition to the above two methods, you can also use the array_filter() function to delete specified elements in the array. This function can filter the elements in the array based on a custom callback function and only retain elements that meet the conditions. If the callback function returns true, the element is retained, otherwise it is deleted. The basic syntax is as follows:

array_filter($array, $callback);

Among them, $array represents the array to be operated on, and $callback represents the callback function.

For example, we still use the array $fruits in the above example. If we want to delete the element named "banana" in the array, we can use the array_filter() function. The code is as follows:

$fruits = array_filter($fruits, function($value){return $value != "banana";});

After running the above code, $fruits will become a new array as shown below:

Array ( [0] => apple [2] => orange [3] => grape )

As you can see, the element named "banana" in the array has been deleted.

Summary

This article introduces three methods to delete specified elements in PHP arrays using the unset() function, array_splice() function and array_filter() function. In actual use, we need to choose the appropriate method according to the specific scenario. If you simply delete an element in the array, you can use the unset() function; if you need to delete and replace elements in the array, you can use the array_splice() function; if you need to delete elements in the array based on custom conditions, you can use array_filter() function.

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