tags:

views:

45

answers:

2

Imgagining i have two array

arr[]={"red","blue","green"}
arr2[]={"red","violet","black"}

How will i compare the two array to get the result that i common to both of the arrays..

For this the result is red...

How wil i do this in php(simple means)...Can anyone suggest???

+3  A: 

To put it quick:

http://php.net/manual/en/function.array-intersect.php

The function takes multiple array and returns an array with ONLY elements that are in every single array.

Multidimensional

This snippet takes all the multidimensional arrays and UNIONS the array together before INTERSECTING the results: ($multi1[0] U $multi1[1] U ... U $multi1[n]) INTERSECT ($multi2[0] U $multi2[1] U ... U $multi2[m]):

$result = null;
foreach(array($multi1, $multi2, $multi3) as $multi) {
    $merge = array();
    foreach($multi as $array) {
      $merge = array_merge($merge, $array);
    }
    if($result == null) {
      $result = $merge;
    } else {
      $result = array_intersect($merge, $result);
    }
}

This snippet takes all the multidimensional arrays and UNIONS the array together before INTERSECTING the results: ($multi1[0] INTERSECT $multi1[1] INTERSECT ... INTERSECT $multi1[n]) INTERSECT ($multi2[0] INTERSECT $multi2[1] INTERSECT ... INTERSECT $multi2[m]):

$result_all = null;
foreach(array($multi1, $multi2, $multi3) as $multi) {
    $result = null;
    foreach($multi as $array) {
      if($result == null) {
        $result = $array;
      } else {
        $result = array_intersect($array, $result);
      }
    }
    if($result_all == null) {
      $result_all = $result;
    } else {
      $result_all = array_intersect($result, $result_all);
    }
}

EDIT - Recursion

To make it simpler to read i made it recursive for the INTERSECT all case:

function array_intersect_multi($arrays, $dim = 1) {
    $result = null;
    foreach($arrays as $array) {
      $tmp = $dim > 1 ? array_intersect_multi($array, $dim - 1) : $array;
      if($result == null) {
        $result = $tmp;
      } else {
        $result = array_intersect($tmp, $result);
      }

    }
    return $result;
}

used like this:

$array1 = array(array('1', '2', '3'), array('3', '5', '0'));
$array2 = array(array('5', '3', '7'), array('3', '7', '8'));
$result = array_intersect_multi(array($array1, $array2), 3);

notice the dimension 3 since the array we are passing in is:

array(
  array(
    array('1', '2', '3'),
    array('3', '5', '0')
  ),
  array(
    array('5', '3', '7'),
    array('3', '7', '8')
  )
)

The result should be:

array('3')

Bob Fincheimer
IF i have a multidimensional array will i be able to implement this(like for example a two dimenstion array)..Can you give me an example on how it works for two dimensional array if possibe;
Sreeja
Don't forget you can use [`array_merge_recursive`](http://www.php.net/manual/en/function.array-merge-recursive.php) for your first snippet.
Austin Hyde
+6  A: 

Use array_intersect function.

$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "violet", "black", "red");
$result = array_intersect($array1, $array2);
print_r($result);

Result:

Array
(
    [0] => red
)
Sarfraz