Several php methods for deleting array elements. Several php methods are used to delete array elements. in many cases, the array will be duplicated. what should we do if we delete some repeated content in the array, I must keep these elements the only several php methods for deleting array elements. in many cases, our array will be duplicated. what should we do if we delete some repeated content in the array, I have to keep these elements unique, so I want to delete them. below are several ways to delete repeated array elements by traversing queries.
Several php tutorials to delete array elements
In many cases, our array will repeat, so what should we do if we delete some repeated content in the array? these elements must be unique, so we can try to delete them, the following describes several methods to delete repeated array elements through traversal queries.
Let's take a look at the complete deletion of duplicate array instances.
// Delete an element from the array
Function array_remove_value (& $ arr, $ var ){
Foreach ($ arr as $ key => $ value ){
If (is_array ($ value )){
Array_remove_value ($ arr [$ key], $ var );
} Else {
$ Value = trim ($ value );
If ($ value = $ var ){
Unset ($ arr [$ key]);
} Else {
$ Arr [$ key] = $ value;
}
}
}
}
$ A is an array:
Count ($ a); // Get 4
Unset ($ a [1]); // deletes the second element.
Count ($ a); // Get 3
Echo $ a [2]; // The array contains only three elements. In this example, we want to get the last element, but we get blue,
Echo $ a [1]; // no value
?>
That is to say, after the elements in the array are deleted, the number of elements in the array (obtained using count () has changed, but the array subscript has not been rearranged, you must also use the key before the array to delete the corresponding value.
Later, I used another method. In fact, it was not called a "method" at all. Instead, I used the php4 ready-made function array_splice ().
Count ($ a); // Get 4
Array_splice ($ a,); // deletes the second element.
Count ($ a); // Get 3
Echo $ a [2]; // obtain yellow
Echo $ a [1]; // Get blue
?>
Method 2
Delete the function of repeating elements in an array
Function delmember (& $ array, $ id)
{
$ Size = count ($ array );
For ($ I = 0; $ I <$ size-$ id-1; $ I ++)
{
$ Array [$ id + $ I] = $ array [$ id + $ I + 1];
}
Unset ($ array [$ size-1]);
}
For more details, see: http://www.bkjia.com/phper/php-function/34794.htm
The distinct method for deleting array elements in many cases will repeat our Array. what should we do if we delete some repeated content in the array? these elements must be unique...