tags:

views:

56

answers:

5

I have two arrays. The first one looks like:

Array1
(
   [14] => foo
   [15] => bar
   [16] => hello
}

and the sencond looks like:

Array2
(
   [Label1] => foo
   [Label2] => bar
   [Label3] => hello
   [Label4] => foo
   [Label5] => bar
}

I would like to compare the values of array1 against array2, if they match, I would like to return the corresponding key of array2.

Any help will be appreciated. Thanks

+2  A: 

You can use array_intersect to get the intersection of both arrays:

$arr1 = array(
    14=>'foo',
    15=>'bar',
    16=>'hello'
);
$arr2 = array(
    'Label1'=>'foo',
    'Label2'=>'bar',
    'Label3'=>'hello',
    'Label4'=>'foo',
    'Label5'=>'bar'
);
var_dump(array_intersect($arr2, $arr1));

This returns:

array(5) {
  ["Label1"]=>
  string(3) "foo"
  ["Label2"]=>
  string(3) "bar"
  ["Label3"]=>
  string(5) "hello"
  ["Label4"]=>
  string(3) "foo"
  ["Label5"]=>
  string(3) "bar"
}

To get the keys of this resulting array, use array_keys. And if you want to get only the first key of each duplicate value, send it through array_unique first:

var_dump(array_keys(array_unique(array_intersect($arr2, $arr1))));

This will get you:

array(3) {
  [0]=>
  string(6) "Label1"
  [1]=>
  string(6) "Label2"
  [2]=>
  string(6) "Label3"
}
Gumbo
A: 

You can go about like this:

$array1 = array(14 => "foo", 15 => "bar", 16 => "hello");
$array2 = array("Label1" => "foo", "Label2" => "bar", "Label3" => "hello", "Label4" => "foo", "Label5" => "bar", "Label5" => "test");
$result = array_intersect($array2, $array1);
echo '<pre>';
print_r($result);

Output:

Array
(
    [Label1] => foo
    [Label2] => bar
    [Label3] => hello
    [Label4] => foo
)

More Info:

Sarfraz
A: 

You might want to check array_intersect_assoc

<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result_array = array_intersect_assoc($array1, $array2);
print_r($result_array); 
?>
The above example will output:
  Array
 (
    [a] => green
 )
andreas
A: 

A simple loop will be much faster and cleaner than a cascade of built-in functions.

//$arr1, $arr2 as per Gumbo's answer...

$hash = array_flip($arr1);
$keys = array();
foreach($arr2 as $key => $val)
    if(isset($hash[$val]))
        $keys[] = $key;
stereofrog
A: 

The way I would approach this is to use a simple "for" iteration:

This gets you all the keys of the values that match.
To get just the very first one you would do a simple modification of this code.


PHP Code:

$arr1 = array(
    14=>'foo',
    15=>'bar',
    16=>'hello'
);
$arr2 = array(
    'Label1'=>'foo',
    'Label2'=>'bar',
    'Label3'=>'hello',
    'Label4'=>'foo',
    'Label5'=>'bar'
);

$results = array();
foreach($arr1 as $val)
  foreach($arr2 as $key=>$val2)
    if($val == $val2) array_push($results, $key);
    // or to get just the first, 
    // replace the if statement with 
    //
    // if($val == $val2) { 
    //   $result = $key;
    //   break 2; 
    // }

print_r($results);

Result is:

array(3) {
  [0] => "Label1",
  [1] => "Label2",
  [2] => "Label3"
}
Dick Savagewood