PHP is a widely used scripting language commonly used for web development. In PHP, array is a very important data structure used to store multiple values. When we need to delete a row in an array, we can use several methods to achieve this goal.
Method 1: Use the unset function
The unset function in PHP can be used to delete elements in an array. We can use the unset function to delete a row of the array. For example:
$my_array = array("apple", "banana", "cherry", "date"); unset($my_array[2]); // 删除数组中的第3个元素,即“cherry”
In the above example, we created an array $my_array and used the unset function to delete the 3rd element of the array (that is, "cherry"). If we use the print_r function to output this array, the output will be as follows:
<br>Array<br>(</p> <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">[0] => apple [1] => banana [3] => date
)
pre>
You can see that the third element ("cherry") in the array has been deleted.
To delete multiple elements in the array, we only need to specify multiple subscripts in the unset function. For example, to delete the 1st and 4th elements in the array $my_array, you can write:
unset($my_array[0], $my_array[3]);
Method 2: Use the array_splice function
Another way to delete array elements is Use array_splice function. This function deletes the specified element from the array and returns the deleted element. For example:
$my_array = array("apple", "banana", "cherry", "date"); $deleted_element = array_splice($my_array, 2, 1); // 返回被删除的元素“cherry”
In the above example, we created an array $my_array and used the array_splice function to delete the 3rd element of the array (that is, "cherry"). The first parameter of the function is the array to be operated on, the second parameter is the index of the element to be deleted, and the third parameter is the number of elements to be deleted (in this example, we only deleted one element). After the function is executed, $my_array will become:
<br>Array<br>(</p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">[0] => apple [1] => banana [2] => date
)
And $deleted_element will be assigned the value "cherry".
If we want to delete more than one element, we can pass in an array as the second parameter. For example, to delete the 1st and 4th elements in the array $my_array, you can write:
$deleted_elements = array_splice($my_array, 0, 1) + array_splice($my_array, 2, 1);
In this example, we first use the array_splice function to delete the 1st element of the array (i.e. "apple"), and then use another array_splice function to delete the 3rd element of the array (which is "date"). The return value of the function will be assigned to the $deleted_elements array, which contains the deleted elements:
<br>Array<br>(</p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">[0] => apple [1] => date
)
< ;/pre>
Method 3: Use the array_diff function
Another way to delete array elements is to use the array_diff function. This function returns the difference between two arrays, which is the result of removing elements from the first array that appear in the second array. For example:
$my_array = array("apple", "banana", "cherry", "date"); $deleted_element = array_diff($my_array, array("cherry")); // 返回删除了“cherry”后的数组
In the above example, we created an array $my_array and used the array_diff function to delete the 3rd element of the array (that is, "cherry"). The first parameter of the function is the array to be operated on, and the second parameter is the array of elements to be deleted. After the function is executed, $deleted_element will be assigned the array after deleting "cherry":
<br>Array<br>(</p> <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">[0] => apple [1] => banana [3] => date
)
< ;/pre>
It should be noted here that the array_diff function can only delete elements that appear in the array, but cannot delete a certain row. If you want to delete a row in the array, you still need to use the unset function or the array_splice function.
Summary
In PHP, we can use the unset function, array_splice function or array_diff function to delete elements in an array. If you want to delete a row, it is recommended to use the unset function or array_splice function, because they can directly delete the element with the specified subscript. If you want to delete multiple elements, it is recommended to use the array_splice function as it can delete multiple elements at once and return the deleted elements.
The above is the detailed content of php remove a row from array. For more information, please follow other related articles on the PHP Chinese website!