There are many ways to delete array elements in php. the built-in functions of php provide us with two functions: array_slice () and array_flip (). you can refer to them. we first use the php function to implement: arr.
There are many ways to delete array elements in php. the built-in functions of php provide us with two functions: array_slice () and array_flip (). you can refer to the following:
Array_slice ()The first parameter is the array to be cut, the second parameter is the starting position, and the third parameter is the length, which is the cut $ arr array, which is counted three after 0th elements, the code is as follows:
Array ("a", "B", "c", "d", "e", "f", "g ")
$ Arr = array_slice ($ arr, 0, 3 );
Array_flip achieves de-duplication effect phpfensi ~ Com
Another method is to use the array_flip function of php to indirectly implement deduplication.
Array_flip is a function used to reverse the array key and value. it has a feature that if two values in the array are the same, the last key and value will be retained after the inversion. we use this feature to indirectly implement array deduplication. the code is as follows:
- $ Arr = array ("a" => "a1", "B" => 'b1 ', "c" => "a2 ", "d" => "a1 ");
- $ Arr1 = array_flip ($ arr );
- Print_r ($ arr1); // first reverse the expression, remove the duplicate value, and output Array ([a1] => d [b1] => B [a2] => c)
- $ Arr2 = array_flip ($ arr );
- Print_r ($ arr2); // returns the array after deduplication, output Array ([a1] => d [b1] => B [a2] => c)
- $ Arr3 = array_unique ($ arr );
- Print_r ($ arr3); // use the array_unique function of php to deduplicate and output Array ([a] => a1 [B] => b1 [c] => a2)
- ?>
The following is a commonly used method unset to delete objects. the code is as follows:
- $ Arr = array ('A', 'B', 'C', 'D ');
- Unset ($ arr [1]);
- Print_r ($ arr );
- ?>
- // After print_r ($ arr), the result is not the same. The final result is Array ([0] => a [2] => c [3] => d