Home > Article > Backend Development > How to delete or clear array elements in php
PHP is a very popular programming language used for web development. In PHP, array is a very important data type that can be used to store multiple values. When we need to delete or clear all elements in an array, PHP provides some built-in functions to easily complete these tasks.
In this article, we will introduce how to delete or clear all elements in an array using PHP built-in functions.
1. Delete array elements
unset function
We can use the PHP built-in function unset() to delete one or more elements in the array. The syntax of the unset() function is as follows:
unset(array[index])
array represents the array of elements to be deleted, and index represents the index of the element to be deleted. For example, we have an array called $fruits, which contains some fruit names. If we want to delete the first element in the array, we can use the following code:
$fruits = array("apple", "banana", "orange"); unset($fruits[0]);
At this time, the elements of the $fruits array will become the following:
Array ( [1] => banana [2] => orange )
array_splice function
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 a specified continuous element and move other elements in the array to a new position. The syntax of the array_splice() function is as follows:
array_splice(array, index, length)
array represents the array to be operated on, index represents the starting index of the element to be deleted, and length represents the number of elements to be deleted. The following is a sample code:
$fruits = array("apple", "banana", "orange", "kiwi"); array_splice($fruits, 1, 2);
At this time, the elements of the $fruits array will become the following:
Array ( [0] => apple [1] => kiwi )
2. Clear the array elements
unset function
We can use the unset() function to delete all elements in the array, thereby emptying the array. The following is a sample code:
$fruits = array("apple", "banana", "orange"); unset($fruits);
At this time, the $fruits array has been destroyed and can no longer be used.
array_filter function
In addition to using the unset() function, we can also use the array_filter() function to clear all elements in the array. The array_filter() function can filter elements in an array based on specified conditions. If no condition is specified, all elements will be filtered out, thus clearing the array. The following is a sample code:
$fruits = array("apple", "banana", "orange"); $fruits = array_filter($fruits);
At this time, the $fruits array has been cleared and turned into an empty array.
Summary:
This article introduces how to use PHP built-in functions to delete or clear all elements in an array. Use the unset() function to delete specified array elements or destroy the entire array. Use the array_splice() function to delete a specified range of consecutive elements. Use the array_filter() function to filter elements in an array and thereby clear the array. You can choose different ways to delete or clear an array in different scenarios.
The above is the detailed content of How to delete or clear array elements in php. For more information, please follow other related articles on the PHP Chinese website!