Home >Backend Development >PHP Tutorial >Summary of methods for removing duplicate values from two-dimensional arrays in PHP
This article mainly introduces a summary of the method of removing duplicate values from a two-dimensional array in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
The specific code is as follows:
Method one:
//二维数组去掉重复值 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; }
Method two:
//二维数组去掉重复值,并保留键值 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]['title'] =$array[1]; $temp2[$k]['keywords'] =$array[2]; $temp2[$k]['content'] =$array[3]; } return $temp2; }
Two PHP methods for removing duplicates from two-dimensional arrays have their own pros and cons. You can choose according to the specific situation.
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHP methods to operate FTP class implementation (upload, download, move, create)
PHP mathematical operation function Function and example analysis
PHP method of calculating the sum and product of values in an array and example analysis
The above is the detailed content of Summary of methods for removing duplicate values from two-dimensional arrays in PHP. For more information, please follow other related articles on the PHP Chinese website!