array_intersect里面的多维数组

时间:2013-01-12 07:36:18

标签: php multidimensional-array array-intersect

我有一个multidimensioanl数组,可以根据用户输入增加。我想在数组内部执行array_intersect以获取键之间的公共值。

例如

Array ( [php] => Array ( [0] => 36 [1] => 51 [2] => 116 [3] => 171 [4] => 215 [5] => 219 [6] => 229 [7] => 247 [8] => 316 ) [java] => Array ( [0] => 14 [1] => 16 [2] => 19 [3] => 24 [4] => 25 [5] => 26 [6] => 29 [7] => 31 [8] => 33 [9] => 34 [10] => 35 [11] => 36 [12] => 37 [13] => 40 [14] => 45 [15] => 49 [16] => 51 ) ) 

预期输出应该像(36,51)

这是我能够得到它

通过这个,

$intersected_array = call_user_func_array('array_intersect', $array);

但是如果我的数组还有第三个键,它们在所有这三个键中都没有共同值

Array ( [php] => Array ( [0] => 36 [1] => 51 [2] => 116 [3] => 171 [4] => 215 [5] => 219 [6] => 229 [7] => 247 [8] => 316 ) [java] => Array ( [0] => 14 [1] => 16 [2] => 19 [3] => 24 [4] => 25 [5] => 26 [6] => 29 [7] => 31 [8] => 33 [9] => 34 [10] => 35 [11] => 36 [12] => 37 [13] => 40 [14] => 45 [15] => 49 [16] => 51 ) [ajax] => Array ( [0] => 91 [1] => 110 [2] => 113 [3] => 172 ) ) 

然后它将输出作为具有最大值的键的结果。在这种情况下,它将返回java键的所有值。

但我的预期输出为0。请帮忙。

1 个答案:

答案 0 :(得分:1)

我复制了你的例子,对我来说它有效:

<?php

$array = array(
    'php' => array(
        36,
        51,
        116,
        171,
        215,
        219,
        229,
        247,
        316,
    ),
    'java' => array(
        14,
        16,
        19,
        24,
        25,
        26,
        29,
        31,
        33,
        34,
        35,
        36,
        37,
        40,
        45,
        49,
        51,
    ),
    'ajax' => array(
        91,
        110,
        113,
        172,
    ),
);

$intersected_array = call_user_func_array('array_intersect',$array);

print_r($intersected_array);

// RESULT: "Array ( ) "

请准确复制此代码并告诉我您的输出。