Intersection of php arrays

Source: Internet
Author: User
Php array intersection

I can't quite understand these methods in the manual. Which of the following experts can explain?

Array_intersect (array1, array2) // Obtain the intersection of the comparison values.
Array_intersect_assoc () // The intersection of the comparison key name and value
Array_intersect_key (array1, array2) // The intersection of comparison key names

The two below are not very clear
Array_intersect_uassoc (array1, array2, array3., function)
Array_intersect_ukey (array1, array2, array3., function)

What is the function return value?

It is better to give an example in practice.


Reply to discussion (solution)

The function parameter indicates that the value or key value in an array, such as array1, array2, and array3, will execute this function.

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' ));

Output result:
Array (2 ){
["Blue"] =>
Int (1)
["Green"] =>
Int (3)
}
These examples are available in the php Manual.

To solve the intersection problem of two arrays, use array_intersect (), array_inersect_assoc, and array_intersect_key. the array_intersect () function is used to calculate the intersection of two numbers.

 "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); ?>


See http://web10000.cn/thread-126-1-1.html for details
Hope to help you

Take the code of #1 as an example.

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' ));
Get
Array(    [blue] => 1    [green] => 3)Array(    [blue] => 1    [green] => 3)

We can see that array_intersect_key uses functions equivalent to key_compare_func internally.
Array_intersect_ukey can change the default behavior through the callback function.
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
We can see that the comparison is no longer case sensitive.

Take the code of #1 as an example.

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' ));
Get
Array(    [blue] => 1    [green] => 3)Array(    [blue] => 1    [green] => 3)

We can see that array_intersect_key uses functions equivalent to key_compare_func internally.
Array_intersect_ukey can change the default behavior through the callback function.
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
We can see that the comparison is no longer case sensitive.

Array_intersect_ukey.
The array_intersect_uassoc function, in the manual, says: "Note: What is different from array_intersect_assoc () is the comparison of key values and key names, function only compares values. The comparison of keys is not mentioned.

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)
See it?

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)
See it?
How can we differentiate whether the function parameter is a 'key' or a 'value '?

No need to differentiate
Array_intersect_uassoc uses the callback function to identify the key.

To determine the value and key, use the array_uintersect_uassoc function.
And pass two callback functions

No need to differentiate
Array_intersect_uassoc uses the callback function to identify the key.

To determine the value and key, use the array_uintersect_uassoc function.
And pass two callback functions

Austria understands. Thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.