Home > Backend Development > PHP Tutorial > How to merge the same elements in an array in php, merge array elements in php_PHP tutorial

How to merge the same elements in an array in php, merge array elements in php_PHP tutorial

WBOY
Release: 2016-07-13 10:14:12
Original
1126 people have browsed it

php method to merge the same elements in an array, php merge array elements

The example in this article describes how to merge the same elements in an array in PHP. Share it with everyone for your reference. The details are as follows:

We have introduced N methods for deleting duplicate arrays. Today’s example is a little different. It deletes the same elements in the array and only retains one same element. The specific example code is as follows:

Copy code The code is as follows:
// Delete the same elements in the array and keep only one same element
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;
}
//Test calling function
$array = array('aa','bb','aa',3,4,5,5,5,5,'bc');
$arr = formatArray($array);
print_r($arr);
?>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/910595.htmlTechArticleHow to merge the same elements in an array in php, merge array elements in php This article describes how to merge the same elements in an array in php method. Share it with everyone for your reference. The details are as follows: About...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template