The array_unique function can be taken seriously. It has this function. Let's take a look at an example of how PHP uses array_unique to reprocess two-dimensional arrays.
Version 5.2.9 of PHP adds array_unique support for multidimensional arrays. In dealing with multidimensional arrays, sort_flags parameters need to be set.
Duplicate terms of one-dimensional arrays:
The array_unique function can be used, with the following examples:
The code is as follows
$aa = array("apple", "banana", "pear", "apple", "wail", "watermalon"); $bb = array_unique($aa); print_r($bb);
The results are as follows:
Array ( [0] => apple [1] => banana [2] => pear [4] => wail [5] => watermalon )
Two-dimensional array de-duplicate terms
The code is as follows
For example:
$result = array( 0=>array('a'=>1,'b'=>'Hello'), 1=>array('a'=>1,'b'=>'other'), 2=>array('a'=>1,'b'=>'other'), );
Processed into
$result = array( 0=>array('a'=>1,'b'=>'Hello'), 1=>array('a'=>1,'b'=>'other') );
Method of use
array_unique($result, SORT_REGULAR);
Duplicates of two-dimensional arrays:
For two-dimensional arrays, we discuss two cases: one is that the value of a key name can not be repeated, deleting duplicate items; the other is that the internal one-dimensional arrays can not be exactly the same, and deleting duplicate items. Here is an example to illustrate:
Delete duplicates because the value of a key name cannot be duplicated
The code is as follows
//Defined function function assoc_unique($arr, $key) { $tmp_arr = array(); foreach ($arr as $k => $v) { if (in_array($v[$key], $tmp_arr)) {//Search for whether $v[$key] exists in the $tmp_arr array and return true if it exists unset($arr[$k]); } else { $tmp_arr[] = $v[$key]; } } sort($arr); //Sort functions sort arrays return $arr; } //Example $aa = array( array('id' => 1, 'name' => 'Zhang San'), array('id' => 1, 'name' => 'Li Si'), array('id' => 2, 'name' => 'Wang Wu'), array('id' => 3, 'name' => 'Zhao Liu'), array('id' => 4, 'name' => 'Zhao Liu') ); $key = 'id'; //Specify key values $news=assoc_unique($aa, $key); echo '<pre>'; var_dump($news);
The results are as follows:
Array ( [0] => Array ( [id] => 1 [name] => Zhang San ) [1] => Array ( [id] => 2 [name] => Wang Wu ) [2] => Array ( [id] => 3 [name] => Zhao Liu ) [3] => Array ( [id] => 4 [name] => Zhao Liu ) )
2. Delete duplicates because the internal one-dimensional arrays are not identical.
The code is as follows
function array_unique_fb($array2D) { foreach ($array2D as $v) { $v = join(",", $v); //Dimension reduction, or implode, converts a one-dimensional array to a comma-connected string $temp[] = $v; } $temp = array_unique($temp);//Remove duplicate strings, that is, duplicate one-dimensional arrays foreach ($temp as $k => $v) { $temp[$k] = explode(",", $v);//Reassemble the disassembled array } return $temp; } $aa = array( array('id' => 123, 'name' => 'Zhang San'), array('id' => 123, 'name' => 'Li Si'), array('id' => 124, 'name' => 'Wang Wu'), array('id' => 123, 'name' => 'Li Si'), array('id' => 126, 'name' => 'Zhao Liu') ); $bb = array_unique_fb($aa); print_r($bb)
Display results:
Array ( [0] => Array ( [0] => 123 [1] => Zhang San ) [1] => Array ( [0] => 123 [1] => Li Si ) [2] => Array ( [0] => 124 [1] => Wang Wu ) [4] => Array ( [0] => 126 [1] => Zhao Liu ) )