How to remove multiple PHP arrays and keep one: 1. Create a PHP sample file; 2. Create the "function formatArray($array) {...}" method; 3. Pass "for($ i=0;$i
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
php Remove multiple arrays and keep one?
php deletes the same elements in the array and keeps only one same element
We have introduced N methods for deleting duplicate arrays. Today’s example is a little different, which is to delete the array. Among the same elements, only one same element is retained. Let’s take an example below. The code is as follows:
<?php // 删除数组中相同元素,只保留一个相同元素 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++;//开源代码phpfensi.com } $tem = $array[$i]; } return $temarray; } //测试 调用函数 $array = array(‘aa’,'bb’,'aa’,3,4,5,5,5,5,’bc’); $arr = formatArray($array); print_r($arr); ?>
Related introduction:
sort() function sorts the numerical array in ascending order.
Tip: Please use the rsort() function to sort the numerical array in descending order.
Grammar
sort(array,sortingtype);
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to remove multiple arrays and keep one in php. For more information, please follow other related articles on the PHP Chinese website!