Home  >  Article  >  Backend Development  >  Find the intersection of two-dimensional arrays in php

Find the intersection of two-dimensional arrays in php

WBOY
WBOYOriginal
2023-05-07 17:47:08957browse

In PHP programming, two-dimensional arrays are one of the frequently used data structures. One of the most commonly used operations on two-dimensional arrays is to find intersection. So, how to find the intersection of two-dimensional arrays in PHP? Let's take a look at the specific implementation.

1. Use the array_intersect function

PHP provides a built-in function array_intersect(), which can be used to find the intersection of two arrays. This function can accept multiple arrays as parameters. For two-dimensional arrays, you need to use the array_map function to convert the associative array into an index array.

The sample code is as follows:

$arr1 = array(
    array('id'=>1,'name'=>'Tom'),
    array('id'=>2,'name'=>'Jack'),
    array('id'=>3,'name'=>'Lucy'),
);

$arr2 = array(
    array('id'=>2,'name'=>'Jack'),
    array('id'=>4,'name'=>'Mike'),
    array('id'=>5,'name'=>'Lily'),
);

$intersect = call_user_func_array('array_intersect', array_map(function($ar){return array_values($ar);},array($arr1, $arr2)));

var_dump($intersect);

The output result is:

array(1) {
  [0]=>
  array(2) {
    ["id"]=>
    int(2)
    ["name"]=>
    string(4) "Jack"
  }
}

2. Use custom functions

In addition to built-in functions, we can also use custom functions function to implement the function of finding the intersection of two-dimensional arrays. The following is a simple implementation:

/**
 * 求二维数组交集
 * @param $arr1
 * @param $arr2
 * @return array
 */
function arr_intersect($arr1, $arr2){
    $intersect = array();
    foreach($arr1 as $value1){
        foreach($arr2 as $value2){
            if($value1 == $value2){
                $intersect[] = $value1;
                break;
            }
        }
    }
    return $intersect;
}

$arr1 = array(
    array('id'=>1,'name'=>'Tom'),
    array('id'=>2,'name'=>'Jack'),
    array('id'=>3,'name'=>'Lucy'),
);

$arr2 = array(
    array('id'=>2,'name'=>'Jack'),
    array('id'=>4,'name'=>'Mike'),
    array('id'=>5,'name'=>'Lily'),
);

$intersect = arr_intersect($arr1, $arr2);

var_dump($intersect);

The output result is:

array(1) {
  [0]=>
  array(2) {
    ["id"]=>
    int(2)
    ["name"]=>
    string(4) "Jack"
  }
}

Summary

The intersection operation of two-dimensional arrays is very common in PHP programming. By using PHP's built-in function array_intersect() or a custom function, we can easily implement the intersection function of two-dimensional arrays. In actual projects, we should choose the optimal method according to specific scenarios to implement a fast and reliable intersection algorithm.

The above is the detailed content of Find the intersection of two-dimensional arrays in php. For more information, please follow other related articles on the PHP Chinese website!

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