Home >Backend Development >PHP Tutorial >Example of implementing one-dimensional array and two-dimensional array deduplication function in PHP
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:
Use## The #array_unique function is sufficient. 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 )
Duplicates of two-dimensional array:
For two-dimensional arrays, we will discuss two situations. 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 array cannot be exactly the same. The following is For example: Because the value of a certain key name cannot be repeated, delete the 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); ?>The displayed result is:
Array ( [0] => Array ( [shopId] => 1 ) [1] => Array ( [shopId] => 2 ))
PS: There are two relatively simple and practical online text deduplication tools on this site, which are recommended to everyone:
Online Duplicate Removal Tool :
http://tools.jb51.net/code/quchong
Online text deduplication tool:
http://tools.jb51.net/aideddesign/txt_quchong
CI framework (CodeIgniter) implementation implementation, Export data operation example
Example of MySQL database backup function implemented by ThinkPHP framework
Implementing WeChat applet face recognition brush through PHP Face login function
The above is the detailed content of Example of implementing one-dimensional array and two-dimensional array deduplication function in PHP. For more information, please follow other related articles on the PHP Chinese website!