After the improvement, you no longer need to traverse according to the length of the array before processing, but directly traverse according to the length of the array after processing
- function unique($array)
- {
- sort($array);
- $arraylength=count($array);
- $endarray=array();
- for ($i=0;$i<$arraylength ;$i++)
- {
- if ($i!="0")
- {
- $nextvalue=$array[$i-1];
- }else{
- $nextvalue="";
- }
- if ($i !=$arraylength)
- {
- $prevalue=$array[$i+1];
- }else{
- $prevalue="";
- }
- $currentvalue=$array[$i];
- if($currentvalue= =$nextvalue||$currentvalue==$prevalue)
- {
- unset($array[$i]);
- }else{
- $endarray[]=$array[$i];
- continue;
- }
- }
- $array=$endarray;
- return $array;
- }
Copy code
|