Home > Backend Development > PHP Problem > How to remove duplicate values ​​from multidimensional array in php

How to remove duplicate values ​​from multidimensional array in php

藏色散人
Release: 2023-03-01 22:36:01
Original
3070 people have browsed it

php method to remove duplicate values: first determine whether to retain the primary and secondary array keys; then convert the one-dimensional array into a string connected with commas; then remove duplicate strings through the "array_unique" function ;Finally, reassemble the disassembled array.

How to remove duplicate values ​​from multidimensional array in php

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 There is a built-in function array_unique () for removing duplicate items from an array, but PHP's array_unique function only applies 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

Demo:

$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

Many related For knowledge, please visit PHP中文网!

The above is the detailed content of How to remove duplicate values ​​from multidimensional array 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