Home  >  Article  >  php教程  >  分享一個PHP函數,用來找出5個Array的交集

分享一個PHP函數,用來找出5個Array的交集

WBOY
WBOYOriginal
2016-06-13 11:29:481687browse

	/**
		數組碰撞,找出多個數組的重疊值
		返回:
			重整之後的Array,如下:
			$arr=Array(
				0=>'',	//5個數組重疊的部份
				1=>'',	//第1個數組去掉[0]的值后
				2=>'',	//第2個數組去掉[0]的值后
				3=>'',	//第3個數組去掉[0]的值后
				4=>'',	//第4個數組去掉[0]的值后
				5=>'',	//第5個數組去掉[0]的值后
			);
		參數:
			要重整的5個數組
	/**/
	Public Static Function JiaoJi($arr1,$arr2,$arr3,$arr4,$arr5) {
		IF(!Is_array($arr1) Or !Is_array($arr2) Or !Is_array($arr3) Or !Is_array($arr4) Or !Is_array($arr5)) {
			throw New Exception('Error:'.__LINE__.',參數錯誤!');Die();
		}

		$arr=Array();
		//找出這5個數組中重複的部份,賦值給$arr[0]
		{
			//合併數組
			$narr=Array_Merge($arr1,$arr2,$arr3,$arr4,$arr5);

			//降維
			Foreach($narr as $v){
				$v = Implode('`',$v);	//降维
				$temp[] = $v;
			}unSet($narr);

			//去重复
			$unique_arr = Array_Unique($temp);

			//取差集
			$repeat_arr = Array_Diff_Assoc($temp,$unique_arr);unSet($temp,$unique_arr);

			//去重複
			$repeat_arr=Array_Unique($repeat_arr);

			//升維
			Foreach($repeat_arr as $k=>$v){
				$v = Explode('`',$v);
				$v['gid']=$v[0];unSet($v[0]);
				$v['cod']=$v[1];unSet($v[1]);
				$v['tid1']=$v[2];unSet($v[2]);
				$v['tid2']=$v[3];unSet($v[3]);
				$v['stime']=$v[4];unSet($v[4]);

				$temp[] = $v;
			}
			$arr[0]=$temp;
			unSet($temp,$repeat_arr);
		}

		/**
		去掉5個數組中包含$arr[0]的部份
		array(5) {
		  ["gid"]=>string(1) "2"
		  ["cod"]=>string(4) "4002"
		  ["tid1"]=>string(3) "184"
		  ["tid2"]=>string(3) "199"
		  ["stime"]=>string(19) "2013-07-25 02:19:00"
		}
		/**/
		$max=Max(Count($arr1),Count($arr2),Count($arr3),Count($arr4),Count($arr5));
		Foreach($arr[0] as $k=>$v) {
			For($i=0;$i<$max;$i++) {
				//去掉arr1和$arr[0]的重複部份
				IF(isSet($arr1[$i])) {
					IF($arr1[$i]['gid']==$v['gid'] And $arr1[$i]['cod']==$v['cod'] And $arr1[$i]['tid1']==$v['tid1'] And $arr1[$i]['tid2']==$v['tid2'] And $arr1[$i]['stime']==$v['stime']) {
						unSet($arr1[$i]);
					}
				}

				//去掉arr2和$arr[0]的重複部份
				IF(isSet($arr2[$i])) {
					IF($arr2[$i]['gid']==$v['gid'] And $arr2[$i]['cod']==$v['cod'] And $arr2[$i]['tid1']==$v['tid1'] And $arr2[$i]['tid2']==$v['tid2'] And $arr2[$i]['stime']==$v['stime']) {
						unSet($arr2[$i]);
					}
				}

				//去掉arr3和$arr[0]的重複部份
				IF(isSet($arr3[$i])) {
					IF($arr3[$i]['gid']==$v['gid'] And $arr3[$i]['cod']==$v['cod'] And $arr3[$i]['tid1']==$v['tid1'] And $arr3[$i]['tid2']==$v['tid2'] And $arr3[$i]['stime']==$v['stime']) {
						unSet($arr3[$i]);
					}
				}

				//去掉arr4和$arr[0]的重複部份
				IF(isSet($arr4[$i])) {
					IF($arr4[$i]['gid']==$v['gid'] And $arr4[$i]['cod']==$v['cod'] And $arr4[$i]['tid1']==$v['tid1'] And $arr4[$i]['tid2']==$v['tid2'] And $arr4[$i]['stime']==$v['stime']) {
						unSet($arr4[$i]);
					}
				}

				//去掉arr5和$arr[0]的重複部份
				IF(isSet($arr5[$i])) {
					IF($arr5[$i]['gid']==$v['gid'] And $arr5[$i]['cod']==$v['cod'] And $arr5[$i]['tid1']==$v['tid1'] And $arr5[$i]['tid2']==$v['tid2'] And $arr5[$i]['stime']==$v['stime']) {
						unSet($arr5[$i]);
					}
				}
			}
		}unSet($max);

		$arr[1]=$arr1;unSet($arr1);
		$arr[2]=$arr2;unSet($arr2);
		$arr[3]=$arr3;unSet($arr3);
		$arr[4]=$arr4;unSet($arr4);
		$arr[5]=$arr5;unSet($arr5);

		Return $arr;
	}

 

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
Previous article:zuitu团购模板引擎浅析Next article:PHP冒泡排序算法