The array_unique function can be used for heavy processing, and it has this function. Let’s take a look at an example about PHP’s use of array_unique to de-duplicate two-dimensional arrays.
The php 5.2.9 version adds array_unique support for multidimensional arrays, and the sort_flags parameter needs to be set when dealing with multidimensional arrays
Duplicates of 1D arrays:
Just use the array_unique function, and the usage examples are 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 to remove duplicates
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'), );
processed into
$result = array( 0=>array('a'=>1,'b'=>'Hello'), 1=>array('a'=>1,'b'=>'other') );
Instructions
array_unique($result, SORT_REGULAR);
Duplicates of 2D arrays:
For two-dimensional arrays, we discuss two cases. One is to delete duplicates because the value of a certain key name cannot be repeated; the other is to delete duplicates because the internal one-dimensional arrays cannot be exactly the same. The following examples illustrate:
(1) Because the value of a key name cannot be repeated, delete the duplicate
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)) {<!-- -->//Search 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 function sorts the array return $arr; } $aa = array( array('id' => 123, 'name' => 'Zhang San'), array('id' => 123, 'name' => 'Li Si'), array('id' => 124, 'name' => 'Wang Wu'), array('id' => 125, 'name' => 'Zhao Liu'), array('id' => 126, 'name' => 'Zhao Liu') ); $key = 'id'; assoc_unique( & amp;$aa, $key); print_r($aa); ?>
The displayed result is:
Array ( [0] => Array ( [id] => 123 [name] => Zhang San ) [1] => Array ( [id] => 124 [name] => Wang Wu ) [2] => Array ( [id] => 125 [name] => Zhao Liu ) [3] => Array ( [id] => 126 [name] => Zhao Liu ) )
(2) Delete duplicates because the internal one-dimensional arrays cannot be exactly the same
The code is as follows Copy code
<?php function array_unique_fb($array2D) { foreach ($array2D as $v) { $v = join(",", $v); //Dimensionality reduction, you can also use implode to convert a one-dimensional array into a string connected by commas $temp[] = $v; } $temp = array_unique($temp);//Remove repeated strings, that is, repeated 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) ?>
Show 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 ) )
Original: https://www.php.cn/php-sourcecode-284972.html
The array_unique function can be used for heavy processing, and it has this function. Let’s take a look at an example about PHP’s use of array_unique to de-duplicate two-dimensional arrays.
The php 5.2.9 version adds array_unique support for multidimensional arrays, and the sort_flags parameter needs to be set when dealing with multidimensional arrays
Duplicates of 1D arrays:
Just use the array_unique function, and the usage examples are as follows:
The code is as follows Copy code
1 2 3 4 5 |
|
The result is as follows:
1 |
|
Two-dimensional array to remove duplicates
The code is as follows Copy code
For example:
1 2 3 4 5 |
|
processed into
1 2 3 4 |
|
Instructions
1 |
|
Duplicates of 2D arrays:
For two-dimensional arrays, we discuss two cases. One is to delete duplicates because the value of a certain key name cannot be repeated; the other is to delete duplicates because the internal one-dimensional arrays cannot be exactly the same. The following examples illustrate:
(1) Because the value of a key name cannot be repeated, delete the duplicate
PHP
The code is as follows Copy Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 twenty one twenty two twenty three twenty four |
|
The displayed result is:
1 |
|
(2) Delete duplicates because the internal one-dimensional arrays cannot be exactly the same
The code is as follows Copy code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 twenty one twenty two twenty three twenty four |
|
Show results:
1 |
|