This article mainly introduces the php method for merging the same elements in an array. it is a very practical technique to use a custom function to traverse the array to merge the same items in the array, for more information about how to merge the same elements in an array in php, see the example in this article. Share it with you for your reference. The details are as follows:
We have introduced N methods for deleting repeated arrays. In this example, the same elements in the array are deleted and only one element is retained. The example code is as follows:
The code is as follows:
<? Php
// Delete the same element from the array and retain only one 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 the call function
$ Array = array ('A', 'BB ', 'AA', 3,4, 5, 5, 5, 'BC ');
$ Arr = formatArray ($ array );
Print_r ($ arr );
?>
I hope this article will help you with php programming.