array_intersect Inside multidimensional-array

2019-03-02 10:23发布

问题:

i have an multidimensioanl array which can increase based on user input.i want to do array_intersect inside the array to get the common values between the key.

like example

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

expected output should be like (36,51)

and this is i am able to get it

via this,

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

but if my array had a third key also which doesnot had a common value in all three of them

like

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

then it is throwing me output as the result of the key having max values. in this case it will return me all the values of java key.

but my expected output is 0. please help.

回答1:

I copyed you example and for me it works:

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

Please copy exactly this code and tell me your output.