Home > Article > Backend Development > How to delete an array in a two-dimensional array in php
PHP, as a Web programming language, often needs to process array data. Sometimes we need to delete an array element in a two-dimensional array because its operation is not needed or has been completed. This article will teach you how to use PHP to delete an array in a two-dimensional array.
1. Background knowledge
Before discussing how to delete array elements in a two-dimensional array, we need to understand some basic PHP syntax and array knowledge.
The array in PHP is an ordered, composite, and variable data structure. There can be different key types, but keys can only be integers or strings. Arrays can be nested, i.e. elements in one array can be elements in another array.
The following is an example of a two-dimensional array:
$twoDimensionArray = array( array("apple", "orange", "banana"), array("red", "orange", "yellow"), array("one", "two", "three") );
In this two-dimensional array, there are three elements, each element is a one-dimensional array containing three strings. Our task is to delete one of the array elements.
2. Methods of deleting an array in a two-dimensional array
There are two methods to delete an array element in a two-dimensional array. The first way is to use a for loop to iterate through the array and remove specific elements, the second way is to use the PHP built-in function array_splice().
Method 1: Use a for loop to traverse an array
Using a for loop to traverse an array can easily delete specific array elements in a two-dimensional array. The following code demonstrates how to delete the second array element (i.e. "array('red','orange','yellow')").
$twoDimensionArray = array( array("apple", "orange", "banana"), array("red", "orange", "yellow"), array("one", "two", "three") ); // 循环遍历数组 for ($i = 0; $i < count($twoDimensionArray); $i++) { // 判断当前元素是否是要删除的元素 if ($twoDimensionArray[$i] === array("red", "orange", "yellow")) { // 删除特定元素 array_splice($twoDimensionArray, $i, 1); } } // 输出结果 var_dump($twoDimensionArray);
The output result is:
array(2) { [0]=> array(3) { [0]=> string(5) "apple" [1]=> string(6) "orange" [2]=> string(6) "banana" } [1]=> array(3) { [0]=> string(3) "one" [1]=> string(3) "two" [2]=> string(5) "three" } }
The method used here is to loop through the entire array to determine whether the current element is the element we want to delete. If it is, use the array_splice() function to remove it. The array_splice() function removes elements from an array and re-indexes the array for next use. In this example, the parameters we pass are:
Method 2: Use the array_splice() function
The array_splice() function is widely used because it can delete one or more elements in the array at one time. The following code demonstrates how to use the array_splice() function to delete the second array element.
$twoDimensionArray = array( array("apple", "orange", "banana"), array("red", "orange", "yellow"), array("one", "two", "three") ); // 使用array_splice()函数删除特定元素 array_splice($twoDimensionArray, 1, 1); // 输出结果 var_dump($twoDimensionArray);
The output result is:
array(2) { [0]=> array(3) { [0]=> string(5) "apple" [1]=> string(6) "orange" [2]=> string(6) "banana" } [1]=> array(3) { [0]=> string(3) "one" [1]=> string(3) "two" [2]=> string(5) "three" } }
Here we only need one line of code to complete the deletion of array elements. The parameters we pass are:
3. Summary
Using PHP to delete an array element in a two-dimensional array is a useful task and a basic array operation. PHP has many functions for operating arrays. We can choose the method that best suits us to delete the array. In this article, we introduced two methods, using a for loop to traverse the array and the array_splice() function. I hope this article can help you better master array operations in PHP.
The above is the detailed content of How to delete an array in a two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!