Home > Backend Development > PHP Tutorial > Example of implementing one-dimensional array and two-dimensional array deduplication function in PHP

Example of implementing one-dimensional array and two-dimensional array deduplication function in PHP

jacklove
Release: 2023-04-01 17:54:01
Original
1675 people have browsed it

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);
?>
Copy after login

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(&#39;shopId&#39; => 1),
  array(&#39;shopId&#39; => 1),
  array(&#39;shopId&#39; => 2),
  array(&#39;shopId&#39; => 2)
);
$key = &#39;id&#39;;
$result = assoc_unique($aa, $key);
print_r($result);
?>
Copy after login

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

Articles you may be interested in:

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template