Heim  >  Artikel  >  Backend-Entwicklung  >  php函数 删除数组中相同的元素_PHP教程

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

WBOY
WBOYOriginal
2016-07-13 17:40:54829Durchsuche

  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. ?>

上面的例子希望能对大家有所收获。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/486182.htmlTechArticle? // 删除数组中相同元素,只保留一个相同元素 function formatArray($array) { sort($array); $tem = ; $temarray = array(); $j = 0; for($i=0;$icount($array);$i ) { if...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn