php Method for merging the same elements in an array

  • 2021-08-03 09:24:51
  • OfStack

This article illustrates how php combines the same elements in an array. Share it for your reference. The details are as follows:

We have introduced N methods about deleting repeated arrays. Today's example is a little different, that is, deleting the same elements in the array, leaving only one same element. The specific example code is as follows:

<?php
// Delete the same elements in the array, leaving only the 1 Same element
function formatArray($array)
{
sort($array);
$tem = "";
$temarray = array();
$j = 0;
for($i=0;$i<count($array);$i++)
{
if($array[$i]!=$tem)
{
$temarray[$j] = $array[$i];
$j++;
}
$tem = $array[$i];
}
return $temarray;
}
// Test Call function
$array = array('aa','bb','aa',3,4,5,5,5,5,'bc');
$arr = formatArray($array);
print_r($arr);
?>

I hope this article is helpful to everyone's php programming.


Related articles: