Methods to remove duplicate values from PHP arrays: 1. Use traversal queries to delete duplicate array elements; 2. Use PHP built-in function "array_unique" to delete duplicate values in the array; 3. Use "array_flip" to achieve deduplication Effect.
Summary of methods to remove duplicate values from php arrays
Several php methods to delete array elements In many cases we There will be duplication in the array, so what should we do if we delete some duplicate content in the array? These elements must remain unique, so we find ways to delete them. Here are several ways to delete duplicate array elements using traversal queries.
Recommended: "PHP Tutorial"
Method 1. Completely delete duplicate array instances-----Delete an element in 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); //得到4 unset($a[1]); //删除第二个元素 count($a); //得到3 echo $a[2]; //数组中仅有三个元素,本想得到最后一个元素,但却得到blue, echo $a[1]; //无值 ?>
That is to say, after deleting the elements in the array, the number of elements in the array (obtained with count()) has changed, but the array subscripts have not been rearranged. The key before deleting the array must be used to operate the corresponding value.
Later I adopted another method. In fact, it was not called a "method" at all. I used the ready-made function array_splice() in php4.
count ($a); //得到4 array_splice($a,1,1); //删除第二个元素 count ($a); //得到3 echo $a[2]; //得到yellow echo $a[1]; //得到blue ?>
Method 2. Function to delete duplicate 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]); }
Supplementary example:
Method 1. PHP has a built-in function array_unique that can be used to delete duplicates in the array Value
array_unique -- Remove duplicate values from the array
array_uniqueDescription
array array_unique (array array)
array_unique() accepts array as input And return a new array without duplicate values
Note that the key names remain unchanged. array_unique() sorts the values first as strings, then retains only the first encountered key for each value, and then ignores all subsequent keys. This does not mean that the first occurrence of the same value in an unsorted array will be preserved.
Note: Two units are considered the same if and only if (string) $elem1 === (string) $elem2. That is, when the expressions of the strings are the same.
The first unit will be retained.
Example: array_unique()
<?php $input = array("a" => "green", "red", "b" => "green", "blue", "red"); $result = array_unique($input); print_r($result); ?>
The above example will output:
Array ( [a] => green [0] => red [1] => blue )
Method 2, array_flip to achieve deduplication effect
Another method is to use php The array_flip function is used to indirectly achieve the deduplication effect
array_flip is a function that reverses the keys and values of an array. It has a characteristic that if two values in the array are the same, then the last one will be retained after the reversal. A key and value. We use this feature to indirectly implement deduplication of the array.
<?php $arr = array("a"=>"a1","b"=>'b1',"c"=>"a2","d"=>"a1"); $arr1 = array_flip($arr); print_r($arr1);//先反转一次,去掉重复值,输出Array ( [a1] => d [b1] => b [a2] => c ) $arr2 = array_flip($arr); print_r($arr2);//再反转回来,得到去重后的数组,输出Array ( [a1] => d [b1] => b [a2] => c ) $arr3 = array_unique($arr); print_r($arr3);//利用php的array_unique函数去重,输出Array ( [a] => a1 [b] => b1 [c] => a2 ) ?>
The difference between the two methods is that the last key and value of the repeated element is obtained by array_flip, and the final key and value of the repeated element is obtained by array_unique. It is the first key and value of two repeated elements.
The above is the detailed content of Summary of methods to remove duplicate values from php arrays. For more information, please follow other related articles on the PHP Chinese website!