php求数组并集的函数

By Symphony - Last updated: Tuesday, April 20, 2010 - Save & Share - Leave a Comment
  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. }
Posted in Uncategorized • Tags: Top Of Page

Write a comment