Home >Backend Development >PHP Problem >How to delete duplicate elements from array in php
Method 1: Use the array_unique() function to deduplicate the array, the syntax is "array_unique($arr)"; 2. Take advantage of the non-repeatable feature of the PHP array key name and use the array_flip() function to reverse the number twice. The key name and key value position of the group, the syntax is "array_flip(array_flip($arr))"; 3. Taking advantage of the non-repeatable feature of the PHP array key name, use the foreach statement to reverse the key name and key value position of the array twice.
The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer
php provides multiple methods to delete duplicate elements of an array. Let's take a look.
Method 1: Use the array_unique() function to deduplicate the array
The array_unique() function is used to remove duplicate values in the array . If two or more array values are the same, only the first value is retained and the other values are removed.
Note: The retained array will retain the key type of the first array item.
<?php header("content-type:text/html;charset=utf-8"); $array = array(1,2,3,3,4,2,3,5,6,4,5,7,8,9,10); var_dump($array); $result = array_unique($array); echo "删除重复元素后的数组"; //var_dump(array_values($filtered_array)); var_dump($result); ?>
Method 2: Use array_flip() function
array_flip() function Used to reverse/exchange the key names in the array and the corresponding associated key values.
It has a feature that if two values in the array are the same, then the last key and value will be retained after the inversion. Using this feature, we use it to indirectly achieve deduplication of the array.
<?php header("content-type:text/html;charset=utf-8"); $a = array(1, 5, 2, 5, 1, 3, 2, 4, 5); // 输出原始数组 echo "原始数组 :"; var_dump($a); // 通过使用翻转键和值移除重复值 $a = array_flip($a); // 通过再次翻转键和值来恢复数组元素 $a = array_flip($a); // 重新排序数组键 $a = array_values($a); // 输出更新后的数组 echo "更新数组 :"; var_dump($a); ?>
Method 3: Use the foreach statement to reverse the key name and key value position of the array twice
Step 1: Define 2 empty arrays to store twice reversed key names and key values
$res1=[]; $res2=[];
Step 2: Use the foreach statement to traverse the original array, and Assign the original array key name and key value to an empty array as the key value and key name
foreach ($array as $k1 => $v1){ //在每次循环中会将当前数组的值赋给 $v1,键名赋给 $k1 $res1[$v1]=$k1; }
You will get an array with reversed key name and key value
Step 3: Use the foreach statement to traverse the reversed array, and assign the key name and key value of the reversed array to another empty array as the key value and key name
foreach ($res1 as $k2 => $v2){ $res2[$v2]=$k2; }
Implementation code
<?php header("content-type:text/html;charset=utf-8"); function f($arr){ var_dump($arr); $res1=[]; $res2=[]; foreach($arr as $k1=>$v1){ $res1[$v1]=$k1; } foreach ($res1 as $k2 => $v2){ $res2[$v2]=$k2; } echo "去重后的数组:"; var_dump($res2); } $arr=array(1,2,3,4,5,4,3,2,1,0); f($arr); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to delete duplicate elements from array in php. For more information, please follow other related articles on the PHP Chinese website!