Home >Backend Development >PHP Tutorial >Remove duplicate elements from array in PHP

Remove duplicate elements from array in PHP

不言
不言Original
2018-04-21 11:57:071758browse

This article introduces the content of deleting duplicate elements of arrays in PHP. It has certain reference value. Now I share it with you. Friends in need can refer to it.

Several methods of deleting array elements in PHP are here In many cases, our arrays will be duplicated. So what should we do if we delete some duplicate contents in the array? These elements must remain unique, so we try to find a way to delete them. The following uses a traversal query to delete several duplicate array elements. method.
Method 1. Completely delete duplicate array instances-----Delete one element in the array




?

1

2

3

4

5

6

7

8

9

10

11

12

13

14


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:




##?

1##That is to say, after deleting the elements in the array, the array The number of elements in (obtained using count()) has changed, but the array subscripts have not been rearranged, and the corresponding value must be manipulated using the key before deleting the array. Later I adopted another method. In fact, it was not called a "method" at all. I used the ready-made

2

3

4

5

6

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


function array_splice()
in php4.



#?


12
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

3

4

5

6





# #?


##1

23
4

5

6

7

8

9

##

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


补充小例子:

方法一、php有内置函数array_unique可以用来删除数组中的重复值

  • array_unique -- 移除数组中重复的值

  • array_unique说明

  • array array_unique ( array array )

  • array_unique() 接受 array 作为输入并返回没有重复值的新数组

注意键名保留不变。array_unique() 先将值作为字符串排序,然后对每个值只保留第一个遇到的键名,接着忽略所有后面的键名。这并不意味着在未排序的 array 中同一个值的第一个出现的键名会被保留。
注: 当且仅当 (string) $elem1 === (string) $elem2 时两个单元被认为相同。就是说,当字符串的表达一样时。
第一个单元将被保留。
例子:array_unique()




?

1

2

3

4

5


<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>


上例将输出:




?

1

2

3

4

5

6


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


方法二、array_flip实现去重效果

另一个方法是使用php的array_flip函数来间接的实现去重效果
array_flip是反转数组键和值的函数,它有个特性就是如果数组中有二个值是一样的,那么反转后会保留最后一个键和值,利用这个特性我们用他来间接的实现数组的去重.




?

1

2

3

4

5

6

7

8

9


<?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 )
?>


二种方法不同的是用array_flip得到的是重复元素最后的键和值,用array_unique得到的是二个重复元素第一个键和值。

希望本文所述对大家学习php程序设计有所帮助,解决数组重复元素问题。

相关推荐:

php之比较运算符

The above is the detailed content of Remove duplicate elements from array in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:php comparison operatorsNext article:php comparison operators