I'm twiting

访问统计

free counters

php求数组并集的函数

  1. /**
  2.  * 求多个数组的并集
  3.  */
  4. function array_union()
  5. {
  6.     $argsCount = func_num_args();
  7.  
  8.     if ($argsCount < 2)
  9.     {
  10.         return false;
  11.     }
  12.     else if (2 == $argsCount)
  13.     {
  14.      list($arr1, $arr2) = func_get_args();
  15.  
  16.      while (list($k, $v) = each($arr2))
  17.      {
  18.          if (!in_array($v, $arr1)) $arr1[] = $v;
  19.      }
  20.  
  21.      return $arr1;
  22.     }
  23.     else // 三个以上的数组合并
  24.     {
  25.         $arg_list = func_get_args();
  26.  
  27.         for ($i = 1; $i < $argsCount; ++$i)
  28.         {
  29.             $args[] = '$arg_list[' . $i . ']';
  30.         }
  31.  
  32.         eval('$all = array_union(' . implode(',',$args) . ');');
  33.         return array_union($arg_list[0], $all);
  34.     }
  35. }
Share on Facebook

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>