Home > Article > Backend Development > How to delete specified elements in php array
As a general-purpose programming language, PHP's array processing function is very powerful. When developing web applications using PHP, you often need to operate arrays, including adding elements to the array, deleting elements from the array, etc. Among them, deleting specified elements from an array is a common requirement. This article will introduce how to use PHP to delete specified elements from an array, helping readers better understand the operation of PHP arrays.
1. Basic knowledge of arrays
Before learning how to delete specified elements from an array, we need to first understand some basic knowledge of PHP arrays. In PHP, array is a very common data type. Its basic syntax format is as follows:
//定义一个数组 $array_name = array(value1, value2, ..., valueN); //访问数组中的元素 $array_name[index]; //其中,index是元素所在的位置
It should be noted that the array index in PHP starts from 0, so the index of the first element is 0 , the second element has index 1, and so on. When defining an array, we can initialize the entire array with the values of the array elements, or we can leave it blank to create an empty array and then add elements to it using array_push() or the [] operator.
2. Use the unset() function to delete the specified element from the array
In PHP, use the unset() function to delete the specified element from the array. The syntax format of this function is as follows:
unset($array_name[index]); //其中,index是要删除的元素的索引
It should be noted that after using the unset() function to delete elements in the array, the indexes in the array will not be reordered, so after the elements are deleted, the indexes in the array Still continuous. The following is a sample code that demonstrates how to use the unset() function to delete specified elements from an array:
//定义一个数组 $fruits = array("apple", "banana", "orange", "peach"); //删除指定的元素 unset($fruits[1]); //删除"banana" //打印剩余的数组元素 print_r($fruits); //输出:Array ( [0] => apple [2] => orange [3] => peach )
In the above sample code, we first define an array $fruits containing four elements, and then use The unset() function deletes the element "banana" with index 1 in the array, and finally prints the remaining array elements. After running this code, the output result is Array ( [0] => apple [2] => orange [3] => peach ), indicating that the "banana" element has been successfully deleted.
3. Use the array_splice() function to delete the specified element from the array
In PHP, you can also use the array_splice() function to delete the specified element from the array. Unlike the unset() function, the array_splice() function can delete multiple elements at the same time and reorder the indices in the array after deleting the elements. The syntax format of this function is as follows:
array_splice($array_name, $start_index, $length);
Among them, $array_name is the array of elements to be deleted; $start_index is the index of the element to be deleted; $length is the number of elements to be deleted. The following is a sample code that demonstrates how to use the array_splice() function to remove specified elements from an array:
//定义一个数组 $fruits = array("apple", "banana", "orange", "peach"); //删除指定的元素 array_splice($fruits, 1, 1); //删除"banana" //打印剩余的数组元素 print_r($fruits); //输出:Array ( [0] => apple [1] => orange [2] => peach )
In the above sample code, we first define an array $fruits containing four elements, and then use The array_splice() function deletes the element "banana" with index 1 in the array, and finally prints the remaining array elements. After running this code, the output result is Array ( [0] => apple [1] => orange [2] => peach ), indicating that the "banana" element has been successfully deleted.
4. Use the array_diff() function to delete the specified element from the array
In addition to using the unset() function and array_splice() function, we can also use the array_diff() function to delete from the array the specified element. The syntax format of this function is as follows:
array_diff($array_name, array(value1, value2, ..., valueN));
Among them, $array_name is the array of elements to be deleted; array(value1, value2, ..., valueN) is the value list of the elements to be deleted. It should be noted that this function does not change the index of the elements in the array, but returns all the elements that are not in the delete list into a new array. The following is a sample code that demonstrates how to use the array_diff() function to delete specified elements from an array:
//定义一个数组 $fruits = array("apple", "banana", "orange", "peach"); //删除指定的元素 $fruits = array_diff($fruits, array("banana")); //打印剩余的数组元素 print_r($fruits); //输出:Array ( [0] => apple [2] => orange [3] => peach )
In the above sample code, we first define an array $fruits containing four elements, and then use The array_diff() function deletes the elements with the value "banana" in the array, and finally prints the remaining array elements. After running this code, the output result is Array ( [0] => apple [2] => orange [3] => peach ), indicating that the "banana" element has been successfully deleted.
5. Summary
This article introduces three commonly used methods, namely using the unset() function, array_splice() function and array_diff() function to delete specified elements from a PHP array. Whether you are developing web applications or working in other fields of programming, you need to master the skills of operating PHP arrays. I hope this article can be helpful to readers when using PHP arrays, allowing everyone to complete programming work more quickly and efficiently.
The above is the detailed content of How to delete specified elements in php array. For more information, please follow other related articles on the PHP Chinese website!