php 数组函数 交集
看手册看到这几个方法,不是很明白。哪位大神帮忙解释一下 function参数表示一个方法就是 array1,array2,array3等数组里面的值或者键值都会去执行这个函数
function key_compare_func ( $key1 , $key2 ){ if ( $key1 == $key2 ) return 0 ; else if ( $key1 > $key2 ) return 1 ; else return - 1 ;} $array1 = array( 'blue' => 1 , 'red' => 2 , 'green' => 3 , 'purple' => 4 ); $array2 = array( 'green' => 5 , 'blue' => 6 , 'yellow' => 7 , 'cyan' => 8 ); var_dump ( array_intersect_ukey ( $array1 , $array2 , 'key_compare_func' ));
求两个数组的交集问题可以使用array_intersect(),array_inersect_assoc,array_intersect_key来实现,其中array_intersect()函数是求两个数的交集
<?PHP $array = array("red"=>"Red","green"=>"red4","Red15"=>"Red",7=>"Level","Width"=>"Red","azzzz1"=>"art","peak"=>158); $array1 = array("red"=>"Red2","greena"=>"red","Red15"=>"Red",7=>"Level","Width"=>"Red","azzzz"=>"art","peak"=>158); $num = array_intersect($array,$array1); print_r ($num); echo ""; $num = array_intersect_assoc($array,$array1); print_r($num); echo ""; $num = array_intersect_key($array,$array1); print_r ($num); ?>
以 #1 的代码为例
function key_compare_func ( $key1 , $key2 ) { if ( $key1 == $key2 ) return 0 ; else if ( $key1 > $key2 ) return 1 ; else return - 1 ;} $array1 = array( 'blue' => 1 , 'red' => 2 , 'green' => 3 , 'purple' => 4 );$array2 = array( 'green' => 5 , 'blue' => 6 , 'yellow' => 7 , 'cyan' => 8 );print_r( array_intersect_key ( $array1 , $array2 ));print_r( array_intersect_ukey ( $array1 , $array2 , 'key_compare_func' ));得
Array( [blue] => 1 [green] => 3)Array( [blue] => 1 [green] => 3)
function key_compare_func ( $key1 , $key2 ) { if ( strtolower($key1) == strtolower($key2) ) return 0 ; else if ( $key1 > $key2 ) return 1 ; else return - 1 ;} $array1 = array( 'Blue' => 1 , 'red' => 2 , 'green' => 3 , 'purple' => 4 );$array2 = array( 'green' => 5 , 'blue' => 6 , 'yellow' => 7 , 'cyan' => 8 );print_r( array_intersect_key ( $array1 , $array2 ));print_r( array_intersect_ukey ( $array1 , $array2 , 'key_compare_func' ));
Array( [green] => 3)Array( [Blue] => 1 [green] => 3可以看到,比较时已不再区分大小写了
以 #1 的代码为例
function key_compare_func ( $key1 , $key2 ) { if ( $key1 == $key2 ) return 0 ; else if ( $key1 > $key2 ) return 1 ; else return - 1 ;} $array1 = array( 'blue' => 1 , 'red' => 2 , 'green' => 3 , 'purple' => 4 );$array2 = array( 'green' => 5 , 'blue' => 6 , 'yellow' => 7 , 'cyan' => 8 );print_r( array_intersect_key ( $array1 , $array2 ));print_r( array_intersect_ukey ( $array1 , $array2 , 'key_compare_func' ));得
Array( [blue] => 1 [green] => 3)Array( [blue] => 1 [green] => 3)
function key_compare_func ( $key1 , $key2 ) { if ( strtolower($key1) == strtolower($key2) ) return 0 ; else if ( $key1 > $key2 ) return 1 ; else return - 1 ;} $array1 = array( 'Blue' => 1 , 'red' => 2 , 'green' => 3 , 'purple' => 4 );$array2 = array( 'green' => 5 , 'blue' => 6 , 'yellow' => 7 , 'cyan' => 8 );print_r( array_intersect_key ( $array1 , $array2 ));print_r( array_intersect_ukey ( $array1 , $array2 , 'key_compare_func' ));
Array( [green] => 3)Array( [Blue] => 1 [green] => 3可以看到,比较时已不再区分大小写了
function key_compare_func ( $key1 , $key2 ) { if ( strtolower($key1) == strtolower($key2) ) return 0 ; else if ( $key1 > $key2 ) return 1 ; else return - 1 ;} $array1 = array( 'Blue' => 1 , 'red' => 2 , 'green' => 3 , 'purple' => 4 );$array2 = array( 'blue' => 1 , 'green' => 5 , 'yellow' => 7 , 'cyan' => 8 );print_r( array_intersect_assoc ( $array1 , $array2 ));print_r( array_intersect_uassoc ( $array1 , $array2 , 'key_compare_func' ));
Array()Array( [Blue] => 1)看出来了吗?
function key_compare_func ( $key1 , $key2 ) { if ( strtolower($key1) == strtolower($key2) ) return 0 ; else if ( $key1 > $key2 ) return 1 ; else return - 1 ;} $array1 = array( 'Blue' => 1 , 'red' => 2 , 'green' => 3 , 'purple' => 4 );$array2 = array( 'blue' => 1 , 'green' => 5 , 'yellow' => 7 , 'cyan' => 8 );print_r( array_intersect_assoc ( $array1 , $array2 ));print_r( array_intersect_uassoc ( $array1 , $array2 , 'key_compare_func' ));
Array()Array( [Blue] => 1)看出来了吗?
不需区分
array_intersect_uassoc 就是用回调函数判别键
如果要自己判断值和键,则要用 array_uintersect_uassoc 函数
并传递两个回调函数
不需区分
array_intersect_uassoc 就是用回调函数判别键
如果要自己判断值和键,则要用 array_uintersect_uassoc 函数
并传递两个回调函数
奥 明白了。 谢谢!