Home > Backend Development > PHP Tutorial > Improve array_unique($array) function

Improve array_unique($array) function

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-25 09:06:30
Original
1151 people have browsed it

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

  1. function unique($array)
  2. {
  3. sort($array);
  4. $arraylength=count($array);
  5. $endarray=array();
  6. for ($i=0;$i<$arraylength ;$i++)
  7. {
  8. if ($i!="0")
  9. {
  10. $nextvalue=$array[$i-1];
  11. }else{
  12. $nextvalue="";
  13. }
  14. if ($i !=$arraylength)
  15. {
  16. $prevalue=$array[$i+1];
  17. }else{
  18. $prevalue="";
  19. }
  20. $currentvalue=$array[$i];
  21. if($currentvalue= =$nextvalue||$currentvalue==$prevalue)
  22. {
  23. unset($array[$i]);
  24. }else{
  25. $endarray[]=$array[$i];
  26. continue;
  27. }
  28. }
  29. $array=$endarray;
  30. return $array;
  31. }
Copy code


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template