Php deletes array elements. There are many ways to delete array elements in php. php provides two built-in functions: array_slice () and array_flip. We first use the php function to implement array_s. There are many ways to delete array elements in php. the built-in php provides two functions: array_slice () and array_flip (). you can refer to them. We first use the php function to implement
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.
It is to cut the $ arr array and count it three times from 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 phperz ~ 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 common method unset to delete objects.
The code is as follows:
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 that. The final result is Array ([0] => a [2] => c [3] => d
For more information about lift () and array_flip (), see. We first use the php function to implement array_s...