-1

I have an array of arrays.

$a = [
    [1, 2, 3],
    [2, 3, 4],
    [2, 4, 5]
];

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

In the above sample data, I'd expect to have a result array containing the value 2 because it is the only value that occurs in all rows.

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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]);
}

8 Comments

@Vivek, Aha. How about using a loop ?
Read the manual php.net/manual/en/function.array-intersect.php it works on as many arrays as you want.
@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])
nice . but I will use foreach loop.
@Anna I see what u mean, looks like a custom function will be needed.
|
1

You can avoid foreach loop by

call_user_func_array('array_intersect',$a);

Comments

0

Unpack the rows into a single array_intersect() call to determine the values which occur in all rows. The result will retain the keys of the qualifying values in the first row of the array.

Code: (Demo)

$array = [
    [1, 2, 3, 4],
    [2, 3, 4, 5],
    [3, 4, 5, 6]
];

var_export(array_intersect(...$array));

Output:

array (
  2 => 3,
  3 => 4,
)

Spreading elements is a valid technique as long as the first level keys are numeric. If the first level keys are not numeric, just call array_values() to re-index the keys before spreading.

var_export(array_intersect(...array_values($array)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.