Home > Article > Backend Development > How to delete duplicate elements in an array in php, _PHP tutorial
Several php methods to delete array elements In many cases, our arrays will be duplicated, then we delete some duplicates in the array What to do with the content? These elements must remain unique, so we have to find ways to delete them. Here are several ways to delete duplicate array elements using traversal queries.
Method 1. Completely delete duplicate array instances-----Delete one 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, and the key before deleting the array must be used to operate accordingly. value.
Later, I adopted another method, which is actually not called a "method" at all. I used PHP4's ready-made function array_splice() .
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 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]); }
Supplementary example:
Method 1. php has a built-in function array_unique that can be used to delete duplicate values in an array
Note that the key name remains 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 elements 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 achieves deduplication effect
Another method is to use PHP’s array_flip function to indirectly achieve the deduplication effect
array_flip is a function that reverses the keys and values of an array. It has a feature that if two values in the array are the same, the last key and value will be retained after the reversal. We use this feature to indirectly implement the array. Remove duplicates.
<?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 using array_flip will get the last key and value of the repeated element, and using array_unique will get it It is the first key and value of two repeated elements.
I hope this article will help you learn PHP programming and solve the problem of repeated elements in arrays.