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 does
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 shows the code:
- $ Arr = array (0, 1, 2, 3, 4 );
- $ Index = 2;
- Unset ($ arr [$ index]);
- Echo count ($ arr );
- If (emptyempty ($ arr [$ index]) {
- Echo "arr [$ index] is empty ";
- }
- Print_r ($ arr );
- /*
- Output:
- 4
- Arr [2] is empty
- Array ([0] => 0 [1] => 1 [3] => 3 [4] =>
- */
From the output result above, we can see that the length of the array is normal, but the subscript remains the original. Therefore, an error occurs when an element is obtained according to the subscript, 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:
- $ 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] =>
- */
To retrieve the array after the first element is deleted, you can use the following two methods:
- // 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 and returns the last element of the array, in the form:
Mixed array_pop (aray target_array );
The following example deletes the last one from the $ states array. the code is as follows:
- $ 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.
The instance code is as follows:
- $ Array = ('a' => "abc", 'B' => "bcd", 'C' => "cde", 'D' => "def ", 'E' => "");
- Array_filter ($ array );
- Echo"
";
- Print_r ($ array );
- ?>
- /* Result:
- Array (
- [A] => abc
- [B] => bcd
- [C] => cde
- [D] => def
- )
- */
I think array_search () is more practical when using several functions.
Like the in_array () function, array_search () finds a key value in the array. if this value is found, the key name of the matching element is returned. if this value is not found, false is returned, the code is as follows:
- $ 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 ');