Home >php教程 >php手册 >php函数 删除数组中相同的元素

php函数 删除数组中相同的元素

WBOY
WBOYOriginal
2016-06-13 10:39:27997browse

  1. //  删除数组中相同元素,只保留一个相同元素
  2. function formatArray($array)
  3. {
  4.      sort($array);
  5.      $tem = ;
  6.      $temarray = array();
  7.      $j = 0;
  8.      for($i=0;$i
  9.      {
  10.         if($array[$i]!=$tem)
  11.         {
  12.              $temarray[$j] = $array[$i];
  13.              $j ;
  14.         }
  15.         $tem = $array[$i];
  16.      }
  17.      return $temarray;
  18. }
  19. //测试 调用函数
  20. $array = array(aa,bb,aa,3,4,5,5,5,5,bc);
  21. $arr = formatArray($array);
  22. print_r($arr);
  23. ?>

上面的例子希望能对大家有所收获。
Statement:
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