多维关联数组交集php

时间:2012-08-29 06:12:05

标签: php multidimensional-array

我有两个多维数组

$array1 = Array (
   [a1] => Array  (
           [a_name] => aaaaa
           [a_value] => aaa
         )

   [b1] => Array (
           [b_name] => bbbbb
           [b_value] => bbb
       )
   [c1] => Array (
           [c_name] => ccccc
           [c_value] => ccc
       )

 )

$array2 = Array (
     [b1] => Array (
           [b_name]=> zzzzz
         )
  )

我想要$array1$array2的交集。结果必须来自$array1

4 个答案:

答案 0 :(得分:10)

function recursive_array_intersect_key(array $array1, array $array2) {
    $array1 = array_intersect_key($array1, $array2);
    foreach ($array1 as $key => &$value) {
        if (is_array($value) && is_array($array2[$key])) {
            $value = recursive_array_intersect_key($value, $array2[$key]);
        }
    }
    return $array1;
}

演示here

答案 1 :(得分:0)

$output = array_intersect_key($array1, $array2);

答案 2 :(得分:0)

PHP中的二维关联数组交集的演示

守则:

<?php
    function compare_states($a1, $a2){
        $diff1 = strcasecmp($a1['state_id'], $a2['state_id']);
        $diff2 = strcasecmp($a1['state_name'], $a2['state_name']);
        if ($diff1 != 0) return $diff1;
        if ($diff2 != 0) return $diff2;
        return 0;
    }
    function calculate_intersection($a1, $a2){
        return array_uintersect($a1, $a2, 'compare_states');
    }
?>

如何运行

<?php
    $a = Array(Array("state_id"=>14, "state_name"=>"Illinois"));
    $b = Array(Array("state_id"=>14, "state_name"=>"Illinois"));
    $new = calculate_intersection($a, $b);
    print_r($a);
    //in this simple case, the intersection is equivalent to $a.
    $a = Array(Array("state_id"=>14, "state_name"=>"Illinois"));
    $b = Array(Array("state_id"=>14, "state_name"=>"Foobar"));
    $new = calculate_intersection($a, $b);
    print_r($a);
    //in this case, the intersection is empty.
?>

单元测试以证明上述代码按设计工作:

$a = Array();
$b = Array();
$new = calculate_intersection($a, $b);
print "\nGroup1\n";
print ((count($new) == count($a) && count($a) == count($b)) ? "." : "FAIL");
print ((count($new) == 0) ? "." : "FAIL");
//==============================================
$a = Array(Array("state_id"=>14, "state_name"=>"Illinois"));
$b = Array(Array("state_id"=>14, "state_name"=>"Illinois"));
$new = calculate_intersection($a, $b);
print "\nGroup2\n";
print ((count($new) == count($a) && count($a) == count($b)) ? "." : "FAIL");
print ((count($new) == 1) ? "." : "FAIL");
print (($new[0]['state_id'] == 14 ? "." : "FAIL"));
print (($new[0]['state_name'] == "Illinois" ? "." : "FAIL"));
//==============================================
print "\nGroup3\n";
$a = Array(Array("state_id"=>14, "state_name"=>"Illinois"), Array("state_id"=> "22", "state_name"=>"Massachusetts"));
$b = Array(Array("state_id"=>14, "state_name"=>"Illinois"), Array("state_id"=> "22", "state_name"=>"Massachusetts"));
$new = calculate_intersection($a, $b);
print ((count($new) == count($a) && count($a) == count($b)) ? "." : "FAIL");
print (($new[0]['state_id'] == 14 ? "." : "FAIL"));
print (($new[0]['state_name'] == "Illinois" ? "." : "FAIL"));
print (($new[1]['state_id'] == 22 ? "." : "FAIL"));
print (($new[1]['state_name'] == "Massachusetts" ? "." : "FAIL"));
//==============================================
$a = Array(Array("state_id"=>"14", "state_name"=>"Illinois"));
$b = Array(Array("state_id"=>"22", "state_name"=>"Massachusetts"));
$new = calculate_intersection($a, $b);
print "\nGroup5\n";
print ((count($new) == 0) ? "." : "FAIL");
//==============================================
$a = Array(Array("state_id"=>"14", "state_name"=>"Illinois"));
$b = Array(Array("state_id"=>"14", "state_name"=>"Illinois"), Array("state_id"=>"22", "state_name"=>"Massachusetts"));
$new = calculate_intersection($a, $b);
print "\nGroup6\n";
print ((count($new) == 1) ? "." : "FAIL");
print (($new[0]['state_id'] == 14) ? "." : "FAIL");
print (($new[0]['state_name'] == "Illinois") ? "." : "FAIL");
//==============================================
$a = Array(Array("state_id"=>"14", "state_name"=>"Illinois"));
$b = Array(Array("state_id"=>"22", "state_name"=>"Massachusetts"), Array("state_id"=>"14", "state_name"=>"Illinois"));
$new = calculate_intersection($a, $b);
print "\nGroup7\n";
print ((count($new) == 1) ? "." : "FAIL");
print (($new[0]['state_id'] == 14) ? "." : "FAIL");
print (($new[0]['state_name'] == "Illinois") ? "." : "FAIL");
//==============================================
$a = Array(Array("state_id"=>"14", "state_name"=>"Illinois"));
$b = Array(Array("state_id"=>"22", "state_name"=>"Massachusetts"), Array("state_id"=>"14", "state_name"=>"Fromulate"));
$new = calculate_intersection($a, $b);
print "\nGroup8\n";
print ((count($new) == 0) ? "." : "FAIL");
//==============================================
$a = Array(Array("state_id"=>"14", "state_name"=>"Illinois"), Array("state_id"=>"14", "state_name"=>"Illinois"));
$b = Array(Array("state_id"=>"14", "state_name"=>"Illinois"), Array("state_id"=>"14", "state_name"=>"Illinois"));
$new = calculate_intersection($a, $b);
print "\nGroup9\n";
print ((count($new) == 2) ? "." : "FAIL");
print (($new[0]['state_id'] == 14) ? "." : "FAIL");
print (($new[0]['state_name'] == "Illinois") ? "." : "FAIL");
print (($new[1]['state_id'] == 14) ? "." : "FAIL");
print (($new[1]['state_name'] == "Illinois") ? "." : "FAIL");
//==============================================
$a = Array(Array("state_id"=>"14", "state_name"=>"Illinois"), Array("state_id"=>"22", "state_name"=>"Massachusetts"));
$b = Array(Array("state_id"=>"22", "state_name"=>"Massachusetts"), Array("state_id"=>"14", "state_name"=>"Illinois"));
$new = calculate_intersection($a, $b);
print "\nGroup7\n";
print ((count($new) == 2) ? "." : "FAIL");
print (($new[0]['state_id'] == 14) ? "." : "FAIL");
print (($new[0]['state_name'] == "Illinois") ? "." : "FAIL");
print (($new[1]['state_id'] == 22) ? "." : "FAIL");
print (($new[1]['state_name'] == "Massachusetts") ? "." : "FAIL");
?>

以上代码打印:

eric@dev $ php a.php
Group1
..
Group2
....
Group3
.....
Group5
.
Group6
...
Group7
...
Group8
.
Group9
.....
Group7
.....

所有点都意味着一切都过去了。

此代码会处理哪些情况?

  1. 两个数组都是空的。交叉路口是空的。
  2. 一个空,另一个带有物品。交叉路口是空的。
  3. 两个数组都有相同的项目,设置:1并设置:n。交点相当于第一个数组。
  4. 除了洗牌之外,两个阵列都是相同的。交叉点相当于第一个数组。
  5. 除了state_name在每种情况下都不同之外,数组是相同的,并且state_id是相同的。交叉口是空的。
  6. 数组a的项目不常见于b,与b共同的项目。数组b的项目不常见,而项目共有的项目。交叉点是a和b共有的项目。
  7. 不做的事情:

    它不测试空值/未定义/不相似的深度数组,也不测试深度为2或更多层的数组。如果数据类型被交换,将字符串与int进行比较,或者将浮点数与octals进行比较,则可能会失败。让PHP正确地完成相等是很困难的。 http://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/

    尽你所能尝试在数据库中完成这项工作,在PHP中进行操作吹喇叭召唤失败的鲸鱼。它效率低下,所以最好不要操作超过几百个项目。令人意外的是,在意外情况下你会惊慌失措,并且很难阅读/理解它在幕后做的事情。

答案 3 :(得分:-1)

我认为你应该检查$ array2 [$ key]是否也是一个数组。

function recursive_array_intersect_key(array $array1, array $array2) {
    $array1 = array_intersect_key($array1, $array2);
    foreach ($array1 as $key => &$value) {
        if (is_array($value) && is_array($array2[$key])) {
            $value = recursive_array_intersect_key($value, $array2[$key]);
        }
    }
    return $array1;
}