2012-02-03 42 views
0

我想找出数组中所有嵌套数组中的重复值。 目前我的数组就是这样的。PHP:检查所有嵌套数组中的重复值

Array $bigarray = Array (
    [431] => Array (
     [0] => orange 
     [1] => apple 
     [2] => pine 
    ) 
    [440] => Array ( 
     [0] => orange 
     [1] => lilly 
    ) 
    [444] => Array ( 
     [0] => orange 
     [1] => pine 
    ) 
) 

我想只提取orange这是在所有

arrays('431','440','444'). 

Woudl你能给我一些想法...? 在此先感谢。

回答

10

您可以使用array_intersect()

$intersected = null; 
foreach ($bigarray as $arr) { 
    $intersected = $intersected ? array_intersect($arr, $intersected) : $arr; 
    if (!$intersected) { 
    break; // no reason to continue 
    } 
} 
print_r($intersected); 

Array 
(
    [0] => orange 
) 
+0

感谢这正是我想要的!顺便说一句,我想知道如何从所有数组中删除提取的项目。我试过foreach($子句作为$ ARR){..未设置。}但没有运气。 – user973067 2012-02-03 13:57:14

1
$output = null; 

foreach ($bigarray as $array) { 
    if (is_null($output)) { 
    $output = $array; 
    continue; 
    } 

    $output = array_intersect($output, $array); 
    if (empty($output)) { 
    break; 
    // there are no common elements in the array 
    } 
} 

var_dump$(output); 
2
$inAllChunks = call_user_func_array('array_intersect',(array_values($bigarray))); 
var_dump($inAllChunks);