PHP two-dimensional array custom function to remove duplicate items_PHP tutorial

WBOY
Release: 2016-07-13 10:48:28
Original
922 people have browsed it

If we are removing duplicate items from one-dimensional data, there is no direct function. However, PHP does not provide functions for two-dimensional data. The editor below will recommend two good two-dimensional arrays to remove duplicate items. Define the function.

Example 1

The code is as follows Copy code


function unique_array_2d($array2D,$stkeep=false,$ndformat=true)
{
// Determine whether to retain the first-level array key (the first-level array key can be non-numeric)
If($stkeep) $stArr = array_keys($array2D);

// Determine whether to retain the secondary array keys (all secondary array keys must be the same)
If($ndformat) $ndArr = array_keys(end($array2D));

//Dimensionality reduction, you can also use implode to convert a one-dimensional array into a string connected with commas
foreach ($array2D as $v){
                    $v = join(",",$v);
                  $temp[] = $v;
}

//Remove repeated strings, that is, repeated one-dimensional arrays
         $temp = array_unique($temp);

//Reassemble the disassembled array
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;
}

$array2D = array(
'first'=>array('title'=>'1111','date'=>'2222'),
'second'=>array('title'=>'1111','date'=>'2222'),
'third'=>array('title'=>'2222','date'=>'3333')
);

echo "

";
print_r(unique_array_2d($array2D,true));

/**
* Deduplicate the two-dimensional array through the specified key
*
* @param array $arr Array to remove duplicates
* @param array $by Specify key to remove duplicates. If this parameter is not specified, array()
will be returned * @return array
*/
function array_multunique($arr,$by = array()) {
       $temp = array();
       foreach($arr as $key => $val) {
               foreach($by as $v) {
                       $temp[$key] .= isset($val[$v]) ? $val[$v] : '';
               }
       }
       return array_intersect_key($arr,array_unique($temp));
}
/*$aa = array ( 
   array ('id' => 123, 'name' => '张三' ),  
   array ('id' => 123, 'name' => '李四' ),  
   array ('id' => 124, 'name' => '王五' ),  
   array ('id' => 125, 'name' => '赵六' ),  
   array ('id' => 126, 'name' => '赵六' )  
); 
$key = 'id'; 
array_multunique ($aa, array('id')); */

function array_remove_key($array,$keys){
       if (!is_array($array) || !is_array($keys)){
               return false;
       }
       foreach($array as $t){
               foreach($keys as $k){
                       unset($t[$k]);
               }
               $doc[]=$t;
       }
       return $doc;

}
/*$array = array(
       '0' => array('a' => 'aaaaa', 'b' => 'bbbbb', 'c' => array('d' => 'ddddd', 'e' => 'eeeee')),
       '1' => array('a' => 'aaaaa', 'b' => 'bbbbb', 'c' => array('d' => 'ddddd', 'e' => 'eeeee'))
);
print_r( array_remove_key($array,array('c')));*/

function array_remove_key_val(&$a,$b,$c){
       foreach ($a as $key=>$value){
               if ( isset($value[$b]) && ($value[$b]==$c) ){
                       unset($a[$key]);
               }
       }
}
/*$a=array(
       array('id'=>1,'num'=>10,'type'=>'news'),
       array('id'=>2,'num'=>100,'type'=>'pic')
);
print_r( array_remove_key_val($a,"id","1") );*/


Example 2

The code is as follows
 代码如下 复制代码

/二维数组去掉重复值
function array_unique_fb($array2D){
     foreach ($array2D as $v){
         $v = join(",",$v);  //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
         $temp[] = $v;
     }
     $temp = array_unique($temp);    //去掉重复的字符串,也就是重复的一维数组
    foreach ($temp as $k => $v){
        $temp[$k] = explode(",",$v);   //再将拆开的数组重新组装
    }
    return $temp;
}


//二维数组去掉重复值  并保留键值
function array_unique_fb($array2D){
    foreach ($array2D as $k=>$v){
        $v = join(",",$v);  //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
  $temp[$k] = $v;
    }
 $temp = array_unique($temp);    //去掉重复的字符串,也就是重复的一维数组
    foreach ($temp as $k => $v){
        $array=explode(",",$v);  //再将拆开的数组重新组装
  $temp2[$k]["id"] =$array[0];  
  $temp2[$k]["litpic"] =$array[1];
  $temp2[$k]["title"] =$array[2];
  $temp2[$k]["address"] =$array[3];
  $temp2[$k]["starttime"] =$array[4];
  $temp2[$k]["endtime"] =$array[5];
  $temp2[$k]["classid"] =$array[6];
  $temp2[$k]["ename"] =$array[7];
 }
    return $temp2;
}

Copy code
/Remove duplicate values ​​from two-dimensional array function array_unique_fb($array2D){

foreach ($array2D as $v){

             $v = join(",",$v); //To reduce dimensionality, you can also use implode to convert a one-dimensional array into a string connected with commas } $temp = array_unique($temp); //Remove repeated strings, that is, repeated one-dimensional arrays foreach ($temp as $k => $v){          $temp[$k] = explode(",",$v); //Reassemble the disassembled array } Return $temp; } //Two-dimensional array removes duplicate values ​​and retains key values function array_unique_fb($array2D){
foreach ($array2D as $k=>$v){            $v = join(",",$v); //Dimensionality reduction, you can also use implode to convert a one-dimensional array into a string connected with commas $temp[$k] = $v;
}
$temp = array_unique($temp); //Remove repeated strings, that is, repeated one-dimensional arrays foreach ($temp as $k => $v){          $array=explode(",",$v); //Reassemble the disassembled array $temp2[$k]["id"] =$array[0]; $temp2[$k]["litpic"] =$array[1]; $temp2[$k]["title"] =$array[2]; $temp2[$k]["address"] =$array[3]; $temp2[$k]["starttime"] =$array[4]; $temp2[$k]["endtime"] =$array[5]; $temp2[$k]["classid"] =$array[6]; $temp2[$k]["ename"] =$array[7]; } Return $temp2; } http://www.bkjia.com/PHPjc/632786.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632786.htmlTechArticleIf we are removing duplicates from one-dimensional data, there is no direct function, but two-dimensional data php No function is provided. Let me recommend two good two-dimensional arrays to repeat...
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!