Home > Backend Development > PHP Problem > How to remove duplicate data in php

How to remove duplicate data in php

藏色散人
Release: 2023-03-15 06:40:02
Original
2333 people have browsed it

php method to remove duplicate data: 1. Remove duplicate data from a one-dimensional array through the array_unique function; 2. Remove duplicate data from a multi-dimensional array through the custom unique_arr method.

How to remove duplicate data in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

php How to remove duplicate data?

PHP removes duplicate array data

<?php
$input = array("a" => "green","", "red","b" => "green", "","blue", "red","c" => "witer","hello","witer");
//$result = array_unique($input); //去除重复元素
$result = a_array_unique($input);   //只留下单一元素
foreach($result as $aa)
{
echo $aa."<br />";
}
function multi_unique($array) {
   foreach ($array as $k=>$na)
       $new[$k] = serialize($na);
   $uniq = array_unique($new);
   foreach($uniq as $k=>$ser)
       $new1[$k] = unserialize($ser);
   return ($new1);
}
function a_array_unique($array)//写的比较好
{
   $out = array();
   foreach ($array as $key=>$value) {
       if (!in_array($value, $out))
{
           $out[$key] = $value;
       }
   }
   return $out;
}
?>
Copy after login

PHP array has a built-in function array_unique () to remove duplicates, but php The array_unique function is only applicable to one-dimensional arrays, not multi-dimensional arrays. The following provides an array_unique function for a two-dimensional array

function unique_arr($array2D,$stkeep=false,$ndformat=true)
{
    // 判断是否保留一级数组键 (一级数组键可以为非数字)
    if($stkeep) $stArr = array_keys($array2D);
    // 判断是否保留二级数组键 (所有二级数组键必须相同)
    if($ndformat) $ndArr = array_keys(end($array2D));
    //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
    foreach ($array2D as $v){
        $v = join(",",$v);
        $temp[] = $v;
    }
    //去掉重复的字符串,也就是重复的一维数组
    $temp = array_unique($temp);
    //再将拆开的数组重新组装
    foreach ($temp as $k => $v)
    {
        if($stkeep) $k = $stArr[$k];
        if($ndformat)
        {
            $tempArr = explode(",",$v);
            foreach($tempArr as $ndkey => $ndval) $output[$k][$ndArr[$ndkey]] = $ndval;
        }
        else $output[$k] = explode(",",$v);
    }
    return $output;
}
Copy after login

Demonstration:

$array2D = array(&#39;first&#39;=>array(&#39;title&#39;=>&#39;1111&#39;,&#39;date&#39;=>&#39;2222&#39;),&#39;second&#39;=>array(&#39;title&#39;=>&#39;1111&#39;,&#39;date&#39;=>&#39;2222&#39;),&#39;third&#39;=>array(&#39;title&#39;=>&#39;2222&#39;,&#39;date&#39;=>&#39;3333&#39;));  
print_r($array2D);  
print_r(unique_arr($array2D,true));
Copy after login

Method 2:

Example 1

1, PHP one-dimensional array to remove duplicates (use array_unique function).

Example 1, code:

<?php
 $aa=array("apple","banana","pear","apple","wail","watermalon");
  $bb=array_unique($aa);
   print_r($bb);
?>
Copy after login

Output result:

Array ( [0] => apple [1] => banana [2] => pear [4] => wail [5] => watermalon )
Copy after login

Second, duplicate items of PHP two-dimensional array:

For two-dimensional arrays, we divide Two situations are discussed, 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.

Example 1, because the value of a certain key name cannot be repeated, delete the duplicates.

Code:

<?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
      { // www.jbxue.com
         unset($arr[$k]);
      }
    else {
        $tmp_arr[] = $v[$key];
      }
    }
  sort($arr); //sort函数对数组进行排序
  return $arr;
  }
  $aa = array(
  array(&#39;id&#39; => 123, &#39;name&#39; => &#39;张三&#39;),
  array(&#39;id&#39; => 123, &#39;name&#39; => &#39;李四&#39;),
  array(&#39;id&#39; => 124, &#39;name&#39; => &#39;王五&#39;),
  array(&#39;id&#39; => 125, &#39;name&#39; => &#39;赵六&#39;),
  array(&#39;id&#39; => 126, &#39;name&#39; => &#39;赵六&#39;)
  );
  $key = &#39;id&#39;;
  assoc_unique(&$aa, $key);
  print_r($aa);
  ?>
Copy after login

Output result:

Array ( [0] => Array ( [id] => 123 [name] => 张三 ) [1] => Array ( [id] => 124 [name] => 王五 ) [2] => Array ( [id] => 125 [name] => 赵六 )
[3] => Array ( [id] => 126 [name] => 赵六 ) )
Copy after login

Example 2, because the internal one-dimensional array cannot be exactly the same, duplicate items are deleted.

Code:

<?php
  function array_unique_fb($array2D){
       foreach ($array2D as $v){
           $v = join(",",$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
           $temp[] = $v;
       } // www.jbxue.com
       $temp = array_unique($temp);    //去掉重复的字符串,也就是重复的一维数组
      foreach ($temp as $k => $v){
          $temp[$k] = explode(",",$v);   //再将拆开的数组重新组装
      }
      return $temp;
  }
  $aa = array(
  array(&#39;id&#39; => 123, &#39;name&#39; => &#39;张三&#39;),
  array(&#39;id&#39; => 123, &#39;name&#39; => &#39;李四&#39;),
  array(&#39;id&#39; => 124, &#39;name&#39; => &#39;王五&#39;),
  array(&#39;id&#39; => 123, &#39;name&#39; => &#39;李四&#39;),
  array(&#39;id&#39; => 126, &#39;name&#39; => &#39;赵六&#39;)
  );
  $bb=array_unique_fb($aa);
  print_r($bb)
  ?>
Array ( [0] => Array ( [0] => 123 [1] => 张三 ) [1] => Array ( [0] => 123 [1] => 李四 ) [2] => Array ( [0] => 124 [1] => 王五 ) [4] => Array ( [0] =>
126 [1] => 赵六 ) )
Copy after login

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to remove duplicate data in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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