Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array of array . Example like

a[0]={1,2,3};
a[1]={2,3,4};
**Edit** in a[2] from a[2]={4,5};
a[2]={2,4,5};
and more 

How can I find common element which exist in all array ?

share|improve this question
add comment

4 Answers

up vote 1 down vote accepted

You can avoid foreach loop by

call_user_func_array('array_intersect',$a);
share|improve this answer
add comment

This is a function I have made. It's just a reference for a multidimensional array.

<?php
$array1 = array('angka'=>12,'satu','2'=>'dua','tiga'=>array('dua','enam','empat'=>array('satu','lima',12)));//object
$array2 = array('dua','tiga','empat',12);//object as intersect refference

function intersect($data=NULL)
{
    if(!empty($data))
    {
        $crashed = array();
        $crashed2 = array();
        foreach($data[0] as $key=>$val)
        {
            if(!is_array($val))
            {
                $crashed[$key] = in_array($val,$data[1]);//return true if crashed (intersect)
            }
            else
            {
                $crashed2[$key] = intersect(array($val,$data[1]));
            }
            $crashed = array_merge($crashed,$crashed2);
        }
    }
    return $crashed;
}
$intersect = intersect(array($array1,$array2));
print_r($intersect);
?>

It returns a result like this:

Array ( [angka] => 1 [0] => [1] => 1 [tiga] => Array ( [0] => 1 [1] => [empat] => Array ( [0] => [1] => [2] => 1 ) ) ) 

It returns true if the value of an array matches with a reference array.

Hope the code can help you.

share|improve this answer
    
thanks for good try :) –  RDC Dec 14 '12 at 11:08
add comment

Have a look here array-intersect. You could use it like this:

$intersect = $a[0];
for ($i = 1; $i < count($a); $i++)
{
    $intersect = array_intersect($intersect, $a[$i]);
}
share|improve this answer
    
that works on only two array . I need on multiple array. –  Vivek Goel Aug 7 '11 at 9:44
    
@Vivek, Aha. How about using a loop ? –  Anna Fr. Aug 7 '11 at 9:45
    
Read the manual php.net/manual/en/function.array-intersect.php it works on as many arrays as you want. –  vascowhite Aug 7 '11 at 9:45
    
@vascowhite that's true .. But probably the length of the array $a is variable, so he can't just do array_intersect($a[0], $a[1], ...., $a[50]) –  Anna Fr. Aug 7 '11 at 9:48
    
nice . but I will use foreach loop. –  Vivek Goel Aug 7 '11 at 9:49
show 6 more comments

As the name suggest, I think you can just use array-intersect

From that page:

<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
?>

gives

Array
(
    [a] => green
    [0] => red
)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.