Home  >  Article  >  Backend Development  >  How to delete array data using php? Introduction to three methods

How to delete array data using php? Introduction to three methods

PHPz
PHPzOriginal
2023-03-28 10:17:344649browse

PHP is a very powerful programming language that is widely used for web development and application development. Arrays are a very common data type in PHP. When working with arrays, sometimes you need to delete some data in the array. This article will explain how to delete array data in PHP.

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

In PHP, you can use the unset() function to delete elements in an array. The unset() function will delete the specified array element from memory.

For example, the following code demonstrates how to use the unset() function to delete specified elements in an array:

$array = array('a', 'b', 'c', 'd', 'e');
unset($array[2]);
print_r($array);

Execute the above code, you will get the following output:

Array
(
    [0] => a
    [1] => b
    [3] => d
    [4] => e
)

In In the above code, we create an array $array, which contains five elements. The third element in the array (i.e., the second element with index 2) is then removed using the unset() function. Finally, the modified array contents are printed using the print_r() function.

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

In addition to using the unset() function, we can also use the array_splice() function to delete elements in the array. The array_splice() function can delete one or more elements from an array and replace the deleted elements with the specified new elements. The basic syntax is as follows:

array_splice(array, start, length)

Among them, array represents the array to be operated on; start specifies the position where to start deleting elements; length specifies The number of elements to be deleted. Specifically, if you want to delete one element, you can set length to 1; if you want to delete multiple elements, set length to the number of elements that need to be deleted.

Next, look at the following example, which demonstrates how to use the array_splice() function to delete an element in the array:

$array = array('red', 'green', 'blue', 'yellow');
array_splice($array, 2, 1);
print_r($array);

Execute the above code, you will get the following output:

Array
(
    [0] => red
    [1] => green
    [3] => yellow
)

In the above code, we create an array containing four elements$array, and then use the array_splice() function to delete the third element (that is, the second element with index position 2 ). Finally, the modified array contents are printed using the print_r() function.

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

In addition to using the above two methods, we can also use the array_filter() function to delete array elements. The array_filter() function can filter out array elements that meet the specified conditions. Specifically, we can use the callback function of the array_filter() function to specify the elements that need to be deleted. The callback function should return a Boolean value. If it returns false, it means the current element is deleted, otherwise the current element is retained.

The following code demonstrates how to use the array_filter() function to delete an element in the array:

$array = array('apple', 'orange', 'banana', 'pear');
$array_new = array_filter($array, function($value) {
    return $value != 'banana';
});
print_r($array_new);

Execute the above code, you will get the following output:

Array
(
    [0] => apple
    [1] => orange
    [3] => pear
)

In the above In the code, we create an array $array containing four elements. The third element in the array (i.e. element 'banana') was then removed using the array_filter() function. Finally, the modified array contents are printed using the print_r() function.

Summary

Deleting array elements is a very common and practical operation in PHP. This article describes three common ways to delete array elements. Use the unset() function to directly delete specified elements from the array. Use the array_splice() function to remove one or more elements from an array and replace them with specified new elements if needed. The array_filter() function can be used to filter out array elements that meet the conditions according to specified conditions, thereby achieving the operation of deleting array elements.

The above is the detailed content of How to delete array data using php? Introduction to three methods. 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