In php, array quantum mechanics is a very useful array type. Next I will summarize some methods to delete array elements and delete elements in the array. The first idea is to use the unset function, it can be deleted, but it may cause problems when operating the array later.
The following example is used to describe:
The Code is as follows: |
Copy code |
$ Arr = array (0, 1, 2, 3, 4 ); $ Index = 2; Unset ($ arr [$ index]); Echo count ($ arr ); If (empty ($ arr [$ index]) { Echo "arr [$ index] is empty "; } Print_r ($ arr ); Output: 4 Arr [2] is empty Array ([0] => 0 [1] => 1 [3] => 3 [4] => |
4) from the output result above, we can see that the length of the array is normal, but the subscript remains the original, so getting the element according to the subscript will cause a problem. In addition, this method is safe for arrays in the key-value format.
How can we safely delete elements? You can use the array_splice function. Array_splice () is used to delete a series of elements specified in the original array and use other values instead (if specified). The return value is the deleted element.
The Code is as follows: |
Copy code |
$ Arr = array (0, 1, 2, 3, 4 ); $ Rtn = array_splice ($ arr, 2, 1 ); Echo count ($ arr ); Print_r ($ arr ); Print_r ($ rtn ); Output: 4 Array ([0] => 0 [1] => 1 [2] => 3 [3] => 4) Array ([0] => |
2) If you want to obtain the array after the first element is deleted, you can use either of the following methods:
The Code is as follows: |
Copy code |
1, $ Arr = array (0, 1, 2, 3, 4 ); Array_splice ($ arr, 0, 1 ); 2, $ Arr = array (0, 1, 2, 3, 4 ); $ Arr = array_splice ($ arr, 1 ); |
The array_shift () function also deletes the first element in the array and returns the value of the deleted element.
The relative array_pop () function deletes the last element in the array.
The array_pop () function deletes the array and returns the last element of the array. The format is:
Mixed array_pop (aray target_array );
The following example deletes the last state from the $ states array:
The Code is as follows: |
Copy code |
$ Fruits = array ("apple", "banana", "orange", "pear "); $ Fruit = array_pop ($ fruits ); // $ Fruits = array ("apple", "banana", "orange "); // $ Fruit = "pear "; |
Array_filter () deletes an empty element from the array.
Function Name: array_filter ()
Call method: array_filter ($ array)
Parameter description: $ array is the operation object. we will delete the null element.
Instance:
The Code is as follows: |
Copy code |
<? Php $ Array = ('A' => "abc", 'B' => "bcd", 'c' => "cde", 'D' => "def ", 'E' => ""); Array_filter ($ array ); Echo "<pre> "; Print_r ($ array ); ?> Result: Array ( [A] => abc [B] => bcd [C] => cde [D] => def ) |
I think array_search () is more practical when using several functions.
The array_search () function is the same as the in_array () function. You can find a key value in the array. If this value is found, the key name of the matching element is returned. If not found, false is returned.
The Code is as follows: |
Copy code |
$ Array = array ('1', '2', '3', '4', '5 ');
$ Del_value = 3; Unset ($ array [array_search ($ del_value, $ array)]); // use unset to delete this element Print_r ($ array ); Output Array ('1', '2', '4', '5 '); |