Home  >  Article  >  Backend Development  >  php合并数组中相同元素的方法_PHP

php合并数组中相同元素的方法_PHP

WBOY
WBOYOriginal
2016-05-31 19:28:00950browse

本文实例讲述了php合并数组中相同元素的方法。分享给大家供大家参考。具体如下:

关于重复数组的删除我们都介绍过N种方法了,今天这个例子有点不同就是 删除数组中相同的元素,只保留一个相同元素,具体实例代码如下:

代码如下:

// 删除数组中相同元素,只保留一个相同元素
function formatArray($array)
{
sort($array);
$tem = "";
$temarray = array();
$j = 0;
for($i=0;$i {
if($array[$i]!=$tem)
{
$temarray[$j] = $array[$i];
$j++;
}
$tem = $array[$i];
}
return $temarray;
}
//测试 调用函数
$array = array('aa','bb','aa',3,4,5,5,5,5,'bc');
$arr = formatArray($array);
print_r($arr);
?>

希望本文所述对大家的php程序设计有所帮助。

Statement:
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