session_start(); $tmp=array( 'a'=>array(0=>'testa',1=>'testb',2=>'testc',3=>'testd',4=>'teste',5=>'testf',6=>'testa'), 'b'=>array(0=>'testa',1=>'testb',2=>'testj',3=>'testh',4=>'testi',5=>'testj'), 'c'=>array(0=>'testa',1=>'testj',2=>'testh',3=>'testl',4=>'testm',5=>'testn'), 'd'=>array(0=>'testa',1=>'testh',2=>'testx',3=>'testy',4=>'testz',5=>'testkk'), ); $rand=rand(0,1000); foreach($tmp as $k=>$v) { echo 'old array'.var_dump($v)."<br><br>"; $res=arrayToDistinct($v,$rand); echo 'result array'.var_dump($res)."<br><br>"; } function arrayToDistinct($newArray,$rand) { $oneArray=@$_SESSION["oneArray".$rand];//整体存储的数组 $realArray=array();//返回的结果数组 $newArray=array_unique($newArray);//本身先去重 if(empty($oneArray)) { $thirdArray=$realArray=$newArray;//第一个返回自身 } else { foreach($newArray as $k=>$v) {//和整体存储的数组进行对比,有就注销变量,然后生成最终结果的数组 if(!in_array($v,$oneArray)){ $realArray[$k]=$v; } } $thirdArray=array_merge($oneArray,$newArray);//把两个数组合并 $thirdArray=array_unique($thirdArray);//合并之后继续去重,生成整体存储的数组 } $_SESSION["oneArray".$rand]=$thirdArray; return $realArray; }