Home > Backend Development > PHP Problem > How to delete duplicate elements in an array in php

How to delete duplicate elements in an array in php

藏色散人
Release: 2023-03-08 15:14:02
Original
2152 people have browsed it

php method to delete duplicate elements in an array: First create a PHP sample file; then delete duplicate elements in the array through "function delmember(&$array, $id){...}".

How to delete duplicate elements in an array in php

#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.

php method to delete duplicate elements in an array

Several php methods to delete array elements In many cases, our arrays will have duplicates, then we delete them in the array What to do with some duplicate 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;
}
}
}
}
Copy after login

$a is an array:

count($a); //得到4
unset($a[1]); //删除第二个元素
count($a); //得到3
echo $a[2]; //数组中仅有三个元素,本想得到最后一个元素,但却得到blue,
echo $a[1]; //无值
?>
Copy after login

That is to say, after deleting the elements in the array, the number of elements in the array (obtained with count()) changes, but the array subscript is not reset. Arrangement, you must also use the key before deleting the array 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
?>
Copy after login

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]);
}
Copy after login

Supplementary example: [Recommended learning: "PHP Video Tutorial"】

Method 1. php has a built-in function array_unique that can be used to delete duplicate values ​​in an array

  • array_unique -- remove duplicate values ​​from the array
  • array_unique description
  • array array_unique (array array)
  • array_unique() accepts an array as input and returns a new array with no 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);
?>
Copy after login

The above example will output:

Array
(
 [a] => green
 [0] => red
 [1] => blue
)
Copy after login

Method 2, array_flip to achieve the duplication 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 the array. It has a characteristic that if there are two in the array The values ​​are the same, then the last key and value will be retained after reversal. Taking advantage of this feature, we use it to indirectly implement deduplication of the array.

<?php
$arr = array("a"=>"a1","b"=>&#39;b1&#39;,"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 )
?>
Copy after login

The difference between the two methods is to usearray_flip gets the last key and value of the repeated element, and uses array_unique to get the first key and value of the two repeated elements.

The above is the detailed content of How to delete duplicate elements in an array in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template