This article mainly introduces PHP to implement the deduplication function of one-dimensional arrays and two-dimensional arrays, and analyzes related operating techniques such as traversing, judging, sorting, and deduplication of one-dimensional arrays and two-dimensional arrays in PHP based on examples. Friends in need can refer to the following
The example of this article describes the implementation of one-dimensional array and two-dimensional array deduplication function in PHP. Share it with everyone for your reference, the details are as follows:
Removal of duplicates in an array
Duplicates in one-dimensional arrays:
Usearray_unique
function is enough, the usage example is as follows:
<?php $aa=array("1","2","3","3","2","watermalon"); $bb=array_unique($aa); print_r($bb); ?>
The result is as follows:
Array ( [0] => 1 [1] => 2 [2] => 3 [5] => watermalon )
Repetition of two-dimensional array Item:
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 completely The same, and delete duplicate items, the following example illustrates:
Because the value of a certain key name cannot be repeated, delete duplicate items
<?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('shopId' => 1), array('shopId' => 1), array('shopId' => 2), array('shopId' => 2) ); $key = 'id'; $result = assoc_unique($aa, $key); print_r($result); ?>
Display The result is:
Array ( [0] => Array ( [shopId] => 1 ) [1] => Array ( [shopId] => 2 ))
The above is the entire content of this article, thank you for reading. Please pay attention to the PHP Chinese website for more information!
Related recommendations:
Example of specifying multi-field sorting for PHP multi-dimensional array
The above is the detailed content of PHP implements one-dimensional array and two-dimensional array deduplication function. For more information, please follow other related articles on the PHP Chinese website!