: This article mainly introduces php array deduplication. For more information about PHP tutorials, see. One-dimensional array
$aa=array("apple","banana","pear","apple","wail","watermalon"); $bb=array_unique($aa); print_r($bb);//Array ( [0] => apple [1] => banana [2] => pear [4] => wail [5] => watermalon
Two-dimensional array
1) because the values of one-click names cannot be repeated, duplicate items are deleted.
$ Aa = array ('id' => 123, 'name' => 'Zhang San'), array ('id' => 123, 'name' => 'Li si'), array ('id' => 124, 'name' => 'Wang 5'), array ('id' => 125, 'name' => 'Zhao Liu'), array ('id' => 126, 'name' => 'Zhao Liu'); // requirement, id cannot be repeated functionassoc_unique ($ arr, $ key) {$ tmp_arr = array (); foreach ($ arras $ k => $ v) {// search for $ v [$ key] in the $ tmp_arr array. If yes, return trueif (in_array ($ v [$ key], $ tmp_arr )) {unset ($ arr [$ k]);} else {$ tmp_arr [] = $ v [$ key] ;}} sort ($ arr ); // the sort function sorts the array. return $ arr;} $ key = 'id'; assoc_unique (& $ aa, $ key); print_r ($ aa ); // Array ([0] => Array ([id] => 123 [name] => Zhang San) [1] => Array ([id] => 124 [name] => Wang Wu) [2] => Array ([id] => 125 [name] => Zhao 6) [3] => Array ([id] => 126 [name] => Zhao 6 ))
2) because the internal one-dimensional arrays cannot be completely the same, the duplicate items are deleted.
$ Aa = array ('id' => 123, 'name' => 'Zhang San'), array ('id' => 123, 'name' => 'Li si'), array ('id' => 124, 'name' => 'Wang 5'), array ('id' => 123, 'name' => 'Li si'), array ('id' => 126, 'name' => 'Zhao Liu'); functionarray_unique_fb ($ array2D) {foreach ($ array2Das $ v) {$ v = join (",", $ v); // dimensionality reduction, you can also use implode, convert a one-dimensional array to a comma-connected string $ temp [] = $ v;} $ temp = array_unique ($ temp); // remove the duplicate string, that is, the repeated one-dimensional array foreach ($ tempas $ k => $ v) {$ temp [$ k] = explode (",", $ v ); // re-assemble the split array} return $ temp;} $ bb = array_unique_fb ($ aa); print_r ($ bb ); // Array ([0] => Array ([0] => 123 [1] => John 3) [1] => Array ([0] => 123 [1] => Li Si) [2] => Array ([0] => 124 [1] => Wang Wu) [4] => Array ([0] => 126 [1] => Zhao 6 ))
Copyright disclaimer: knowledge is used by the people! You are welcome to repost this article. For more information, see the link at the beginning!
The above introduces php array de-duplication, including some content, hope to be helpful to friends who are interested in PHP tutorials.