PHP implements one-dimensional array and two-dimensional array deduplication function

不言
Release: 2023-03-28 21:56:01
Original
1296 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:

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

The result is as follows:

Array ( [0] => 1 [1] => 2 [2] => 3 [5] => watermalon )
Copy after login

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(&#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

Display The result is:

Array ( [0] => Array ( [shopId] => 1 ) [1] => Array ( [shopId] => 2 ))
Copy after login

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!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!