There are many ways to delete array elements in PHP, PHP built-in for us to provide two functions array_slice () and Array_flip () you can refer to. Let's first use PHP functions to implement
Array_slice () The first parameter is the array to be cut, the second argument is the starting position, and the third parameter is the length.
is to cut $arr this array, from the No. 0 element back to the number of 3.
The code is as follows |
Copy Code |
Array ("A", "B", "C", "D", "E", "F", "G") $arr = Array_slice ($arr, 0, 3); |
Array_flip to achieve de-weight effect phperz~com
Another method is to use PHP's Array_flip function to indirectly implement the redo effect.
Array_flip is a function that reverses array keys and values, and he has a feature that if two values are the same in an array, the last key and value are preserved after the reversal. Using this feature we use him to indirectly implement the weight of the array.
The code is as follows |
Copy Code |
$arr = Array ("A" = "A1", "B" = "B1", "c" = "A2", "D" = "A1"); $arr 1 = array_flip ($arr); Print_r ($arr 1);//Invert once, remove duplicate values, output array ([A1] + d [B1] + b [a2] = c) $arr 2 = Array_flip ($arr); Print_r ($arr 2);//invert back, get the array to go back, Output Array ([A1] + d [B1] = b [A2] + c) $arr 3 = Array_unique ($arr); Print_r ($arr 3);//use PHP's Array_unique function to go heavy, output array ([a] = a1 [b] = B1 [c] = = A2) ?> |
Here is the method unset I used to implement the deletion
The code is as follows:
The code is as follows |
Copy Code |
$arr = Array (' A ', ' B ', ' C ', ' d '); Unset ($arr [1]); Print_r ($arr); ?> |
Print_r ($arr), the result is not that, the end result is an Array ([0] = a [2] = c [3] + D
http://www.bkjia.com/PHPjc/631614.html www.bkjia.com true http://www.bkjia.com/PHPjc/631614.html techarticle There are many ways to delete array elements in PHP, PHP built-in for us to provide two functions array_slice () and Array_flip () you can refer to. We first use PHP function to implement array_s ...