1

我有一个多维数组,可以根据用户输入增加。我想在数组内部做 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。请帮忙。

4

1 回答 1

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 ( ) "

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

于 2013-01-12T07:52:50.003 回答