Home >php教程 >PHP源码 >PHP uses array_unique to deduplicate two-dimensional arrays

PHP uses array_unique to deduplicate two-dimensional arrays

不言
不言Original
2018-05-24 10:39:0083334browse

The array_unique function can handle duplicates. It has this function. Let’s take a look at an example of using array_unique in PHP to deduplicate a two-dimensional array.

php 5.2.9 version adds array_unique support for multi-dimensional arrays. When processing multi-dimensional arrays, you need to set the sort_flags parameter

Duplicates of one-dimensional arrays:

Use array_unique The function is enough, the usage example is as follows:

The code is as follows Copy code

<?php
$aa = array("apple", "banana", "pear", "apple", "wail", "watermalon");
$bb = array_unique($aa);
print_r($bb);
?>

The result is as follows:

Array ( [0] => apple [1] => banana [2] => pear [4] => wail [5] => watermalon ) 。

Two-dimensional array deduplication items

The code is as follows Copy code

For example:

$result = array(
0=>array(&#39;a&#39;=>1,&#39;b&#39;=>&#39;Hello&#39;),
1=>array(&#39;a&#39;=>1,&#39;b&#39;=>&#39;other&#39;),
2=>array(&#39;a&#39;=>1,&#39;b&#39;=>&#39;other&#39;),
);

is processed into

$result = array(
0=>array(&#39;a&#39;=>1,&#39;b&#39;=>&#39;Hello&#39;),
1=>array(&#39;a&#39;=>1,&#39;b&#39;=>&#39;other&#39;) 
);

Using method

array_unique($result, SORT_REGULAR);

Duplicates of a two-dimensional array :

We will discuss two-dimensional arrays in two situations. One is because the value of a certain key name cannot be repeated, and duplicates are deleted; the other is because the internal one-dimensional array cannot be exactly the same, and it is deleted. Duplicate items, the following example illustrates:

㈠Because the value of a certain key name cannot be repeated, delete duplicate items

PHP

The code is as follows Copy code

<?php
function assoc_unique($arr, $key) {
$tmp_arr = array();
foreach ($arr as $k => $v) {
if (in_array($v[$key], $tmp_arr)) {//搜索$v[$key]是否在$tmp_arr数组中存在,若存在返回true
unset($arr[$k]);
} else {
$tmp_arr[] = $v[$key];
}
}
sort($arr); //sort函数对数组进行排序
return $arr;
}
$aa = array(
array(&#39;id&#39; => 123, &#39;name&#39; => &#39;张三&#39;),
array(&#39;id&#39; => 123, &#39;name&#39; => &#39;李四&#39;),
array(&#39;id&#39; => 124, &#39;name&#39; => &#39;王五&#39;),
array(&#39;id&#39; => 125, &#39;name&#39; => &#39;赵六&#39;),
array(&#39;id&#39; => 126, &#39;name&#39; => &#39;赵六&#39;)
);
$key = &#39;id&#39;;
assoc_unique(&$aa, $key);
print_r($aa);
?>

The displayed result is:

Array ( [0] => Array ( [id] => 123 [name] => 张三 ) [1] => Array ( [id] => 124 [name] => 王五 ) [2] => Array ( [id] => 125 [name] => 赵六 ) [3] => Array ( [id] => 126 [name] => 赵六 ) )

㈡Because the internal one-dimensional arrays cannot be exactly the same, duplicate items are deleted

The code is as follows Copy code

<?php

function array_unique_fb($array2D) {
foreach ($array2D as $v) {
$v = join(",", $v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
$temp[] = $v;
}
$temp = array_unique($temp);//去掉重复的字符串,也就是重复的一维数组
foreach ($temp as $k => $v) {
$temp[$k] = explode(",", $v);//再将拆开的数组重新组装
}
return $temp;
}

$aa = array(
array(&#39;id&#39; => 123, &#39;name&#39; => &#39;张三&#39;),
array(&#39;id&#39; => 123, &#39;name&#39; => &#39;李四&#39;),
array(&#39;id&#39; => 124, &#39;name&#39; => &#39;王五&#39;),
array(&#39;id&#39; => 123, &#39;name&#39; => &#39;李四&#39;),
array(&#39;id&#39; => 126, &#39;name&#39; => &#39;赵六&#39;)
);
$bb = array_unique_fb($aa);
print_r($bb)
?>

Display results:

Array ( [0] => Array ( [0] => 123 [1] => 张三 ) [1] => Array ( [0] => 123 [1] => 李四 ) [2] => Array ( [0] => 124 [1] => 王五 ) [4] => Array ( [0] => 126 [1] => 赵六 ) )
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