PHP obtains the example of repeated elements in the array. 1. obtain repeated elements in an array. Code: a. Method 1: Code: functionarray_repeat ($ arr) {if (! Is_array ($ arr) return $ arr; $ arr1array_count_values ($ arr); $ n 1. obtain repeated elements in an array. The code is as follows:
A. Method 1:
| The code is as follows: |
|
|
Function array_repeat ($ arr)
{
If (! Is_array ($ arr) return $ arr;
$ Arr1 = array_count_values ($ arr );
$ NewArr = array ();
Foreach ($ arr1 as $ k => $ v)
{
If ($ v> 1) array_push ($ newArr, $ k );
}
Return $ newArr;
} |
B. Method 2:
| The code is as follows: |
|
|
Function array_repeat ($ arr)
{
If (! Is_array ($ arr) return $ arr;
$ Arr1 = array_unique ($ arr );
$ Arr3 = array_diff_key ($ arr, $ arr1 );
Return array_unique ($ arr3 );
} |
Test method
| The code is as follows: |
|
|
$ Arr = array ('apple', 'blue', 'red', 'banana ', 'pear', 'apple', 'blue', 'Color ', 'color ');
Print_r (array_repeat ($ arr ));
Result: Array ([5] => apple [6] => blue [8] => color) |
Later, I used some methods to delete repeated array elements.
The array_keys function is used to delete repeated array elements.
*/
| The code is as follows: |
|
|
$ A = array ("red", "green", "blue", "yellow ");
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
// Array array_splice (array input, int offset [, int length [, array replacement])
// Array_splice () is actually a function to replace array elements. However, if no replacement value is added, the elements are simply deleted. the usage of array_splice () is as follows:
$ B = array ("red", "green", "blue", "yellow ");
Array_splice ($ a, 1, 1 ); |
// The following describes how to comprehensively delete duplicate values and delete specified array elements.
| The code is as follows: |
|
|
$ Array1 = array (1 => "www.45it.net", 2 => "pineapple", 4 => "www.45it.net", 3 => "banana", 4 => "Ba Le ", 5 => "www.45it.net", 6 => "www.45it.net ");
$ Search_keys = array_keys ($ array1, "www.45it.net ");
Foreach ($ search_keys as $ key ){
Unset ($ array1 [$ key]);
}
Print_r ($ array1 );
/*
Expected result
Array ([2] => pineapple [4] => Ba Le [3] => bananas)
*/
// Delete the function of repeating elements in the 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]);
} |
Bytes. The code is as follows: a. Method 1: The code is as follows: function array_repeat ($ arr) {if (! Is_array ($ arr) return $ arr; $ arr1 = array_count_values ($ arr); $ n...