PHP can delete specific elements in an array. Method 1: Copy the code as follows :? Php $ arr1array (1, 3, 5, 7, 8); $ keyarray_search (3, $ arr1); if ($ key! False) array_splice ($ arr1, $ key, 1); var_dump ($ arr1 );? Output: Method 1:
The code is as follows:
$ Arr1 = array (1, 3, 5, 7, 8 );
$ Key = array_search (3, $ arr1 );
If ($ key! = False)
Array_splice ($ arr1, $ key, 1 );
Var_dump ($ arr1 );
?>
Output:
Array (4) {[0] => int (1) [1] => int (5) [2] => int (7) [3] => int (8 )}
Method 2:
The code is as follows:
$ Arr2 = array (1, 3, 5, 7, 8 );
Foreach ($ arr2 as $ key => $ value)
{
If ($ value = 3)
Unset ($ arr2 [$ key]);
}
Var_dump ($ arr2 );
?>
Output:
Array (4) {[0] => int (1) [2] => int (5) [3] => int (7) [4] => int (8 )}
Conclusion: We can see that there is a difference between deleting a specific value using array_splice () and deleting a specific value using unset.
If the array_splice () function is deleted, the index value of the array also changes.
If the unset () function is deleted, the index value of the array remains unchanged.
The pipeline code is as follows :? Php $ arr1 = array (1, 3, 5, 7, 8); $ key = array_search (3, $ arr1); if ($ key! = False) array_splice ($ arr1, $ key, 1); var_dump ($ arr1 );? Output :...