PHP如何获取两个或更多数组的交集

时间:2015-07-14 14:54:19

标签: php arrays

我试图用这种结构得到两个或多个数组的交集:

第一个阵列:

array(
    [0] => array(
        ['room_id'] => 21
        ['room_name'] => 'GB 101'
        ['capacity'] => 40
    )
    [1] => array(
        ['room_id'] => 22
        ['room_name'] => 'H 114'
        ['capacity'] => 20
    )
    [2] => array(
        ['room_id'] => 23
        ['room_name'] => 'GB 203'
        ['capacity'] => 20
    )
    [3] => array(
        ['room_id'] => 25
        ['room_name'] => 'H 100'
        ['capacity'] => 30
    )
    [4] => array(
        ['room_id'] => 26
        ['room_name'] => 'GB 206'
        ['capacity'] => 40
    )
)

第二阵列:

array(
    [0] => array(
        ['room_id'] => 21
        ['room_name'] => 'GB 101'
        ['capacity'] => 40
    )
    [1] => array(
        ['room_id'] => 23
        ['room_name'] => 'GB 203'
        ['capacity'] => 20
    )
    [2] => array(
        ['room_id'] => 26
        ['room_name'] => 'GB 206'
        ['capacity'] => 40
    )
)

产生的数组:

array(
    [0] => array(
        ['room_id'] => 21
        ['room_name'] => 'GB 101'
        ['capacity'] => 40
    )
    [1] => array(
        ['room_id'] => 23
        ['room_name'] => 'GB 203'
        ['capacity'] => 20
    )
    [2] => array(
        ['room_id'] => 26
        ['room_name'] => 'GB 206'
        ['capacity'] => 40
    )
)

我尝试使用array_intersect_assoc使用以下代码获取交集:

$result = call_user_func_array('array_intersect_assoc', $arrays);

它可以解决问题,但它会根据here提供以下警告:

  

遇到PHP错误

     

严重性:注意

     

消息:数组到字符串转换

我正在制作一个基于Ajax的系统,所以错误搞砸了它。有没有办法得到数组的交集?

3 个答案:

答案 0 :(得分:1)

尝试序列化它们:

$result = array_map('unserialize',
              array_intersect(
                  array_map('serialize', $first), array_map('serialize', $second)));

答案 1 :(得分:0)

使用array_uintersect()来使用自定义比较功能,例如按room_id进行比较。

<?php
$array1 = array(
    "0" => array(
        "room_id" => 21,
        "room_name" => 'GB 101',
        "capacity" => 40
    ),
    "1" => array(
        "room_id" => 22,
        "room_name" => 'H 114',
        "capacity" => 20
    ),
    "2" => array(
        "room_id" => 23,
        "room_name" => 'GB 203',
        "capacity" => 20
    ),
    "3" => array(
        "room_id" => 25,
        "room_name" => 'H 100',
        "capacity" => 30
    ),
    "4" => array(
        "room_id" => 26,
        "room_name" => 'GB 206',
        "capacity" => 40
    )
);

$array2 = array(
    "0" => array(
        "room_id" => 21,
        "room_name" => 'GB 101',
        "capacity" => 40
    ),
    "1" => array(
        "room_id" => 23,
        "room_name" => 'GB 203',
        "capacity" => 20
    ),
    "2" => array(
        "room_id" => 26,
        "room_name" => 'GB 206',
        "capacity" => 40
    )
);

$intersect = array_uintersect($array1, $array2, 'compareDeepValue');

function compareDeepValue($val1, $val2)
{
   return strcmp($val1['room_id'], $val2['room_id']);
}

echo "<pre>";
print_r($intersect);

?>

输出:

Array
(
    [0] => Array
        (
            [room_id] => 21
            [room_name] => GB 101
            [capacity] => 40
        )    
    [1] => Array
        (
            [room_id] => 23
            [room_name] => GB 203
            [capacity] => 20
        )    
    [2] => Array
        (
            [room_id] => 26
            [room_name] => GB 206
            [capacity] => 40
        )    
)

答案 2 :(得分:0)

要比较两个多维数组,一个简单的解决方案就是

$new_array = array();
foreach($array1 as $sub_array1)
{
   foreach($array2 as $sub_array2)
   {
       if($sub_array1 === $sub_array2)
       {
            $new_array[] = $sub_array2;
       }
   }
}

print_r($new_array);