Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-06 20:17:37
Original
1440 people have browsed it

这篇文章主要介绍了php合并数组中相同元素的方法,通过一个自定义函数遍历数组实现数组中相同项的合并,是非常实用的技巧,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

Related labels:
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 Recommendations
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!