<?php /** *7.通过回调函数得到数组交集或差集 */ /*array_udiff()比较2个或多个数组的差值,只比较值,不进行键名的比较 * */ /*$array1 = array("a" => "a", "b" => 'b', "c" => "c1"); $array2 = array("a" => "a", "b" => 'b', "c" => "c"); function func($v1, $v2) { if ($v1 === $v2){ return 0; }elseif ($v1>$v2){ return 1; }else{ return -1; }; } $array3 = array_udiff($array1, $array2, "func"); print_r($array3);//输出:Array ( [c] => c1 )*/ /*array_udiff_assoc() 通过回调函数,比较2个或多个数组的键名或键值的差值。 * */ /*$array1 = array("a1" => "a", "b" => 'b1', "c" => "c"); $array2 = array("a" => "a", "b" => 'b', "c" => "c"); function fun1($v1,$v2){ if ($v1 === $v2){ return 0; }elseif ($v1>$v2){ return 1; }else{ return -1; }; } $array3=array_udiff_assoc($array1,$array2,"fun1"); print_r($array3);//输出:Array ( [a1] => a [b] => b1 )*/ /*array_udiff_uassoc() 通过回调函数,比较多个数组的键名和键值,要定义2个回调函数来处理键名和键值的比较 * */ /*$array1 = array("a1" => "a", "b" => 'b1', "c" => "c"); $array2 = array("a" => "a", "b" => 'b', "c" => "c"); function func_date($v1,$v2){ if ($v1 === $v2){ return 0; }elseif ($v1>$v2){ return 1; }else{ return -1; }; } function func_key($v1,$v2){ if ($v1 === $v2){ return 0; }elseif ($v1>$v2){ return 1; }else{ return -1; }; } $array3=array_udiff_uassoc($array1,$array2,"func_date","func_key"); print_r($array3);//输出:Array ( [a1] => a [b] => b1 )*/ /*array_uintersect() 得到多个数组中键值的交集,通过回调函数: * */ /*$array1 = array("a" => "a1", "b" => 'b1', "c" => "c"); $array2 = array("a" => "a1", "b" => 'b', "c" => "c"); function func5($v1, $v2) { if ($v1 === $v2) { return 0; } elseif ($v1 >$v2) { return 1; } else { return -1; }; } $array3 = array_uintersect($array1, $array2, "func5"); print_r($array3);//输出:Array ( [a] => a1 )*/ /*array_uintersect_assoc() 得到多个数组中的键值的交集,通过函数,键名也参与比较 * */ /*$array1 = array("a" => "a", "b" => 'b1', "c" => "c"); $array2 = array("a" => "a", "b" => 'b', "c1" => "c"); function func6($v1, $v2) { if ($v1 === $v2) { return 0; } elseif ($v1 > $v2) { return 1; } else { return -1; }; } $array3 = array_uintersect_assoc($array1, $array2, "func6"); print_r($array3);//输出:Array ( [a] => a )*/ /*array_uintersect_uassoc() 通过2个回调函数,对多个数组的键名和键值进行比较,得到多个数组的交集元素。 * */ $array1 = array("a" => "a", "b" => 'b1', "c" => "c"); $array2 = array("a" => "a", "b" => 'b', "c1" => "c"); function func_data($v1, $v2) { if ($v1 === $v2) { return 0; } elseif ($v1 > $v2) { return 1; } else { return -1; }; } function func_key($v1, $v2) { if ($v1 === $v2) { return 0; } elseif ($v1 > $v2) { return 1; } else { return -1; }; } $array3 = array_uintersect_uassoc($array1, $array2, "func_data","func_key"); print_r($array3);//输出:Array ( [a] => a )