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.
http://www.bkjia.com/PHPjc/910595.htmlwww.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...