PHP uses array_unique to deduplicate two-dimensional arrays
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('a'=>1,'b'=>'Hello'), 1=>array('a'=>1,'b'=>'other'), 2=>array('a'=>1,'b'=>'other'), );
is processed into
$result = array( 0=>array('a'=>1,'b'=>'Hello'), 1=>array('a'=>1,'b'=>'other') );
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('id' => 123, 'name' => '张三'), array('id' => 123, 'name' => '李四'), array('id' => 124, 'name' => '王五'), array('id' => 125, 'name' => '赵六'), array('id' => 126, 'name' => '赵六') ); $key = 'id'; 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('id' => 123, 'name' => '张三'), array('id' => 123, 'name' => '李四'), array('id' => 124, 'name' => '王五'), array('id' => 123, 'name' => '李四'), array('id' => 126, 'name' => '赵六') ); $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] => 赵六 ) )

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools