1

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
  • 1
    Would you want the “first” key that has the same value or every key? Commented Aug 3, 2010 at 16:23
  • "first" key that has the same value. So basically iterate each one on array1 and if the value matches array2 return that key from array2. Commented Aug 3, 2010 at 16:28

5 Answers 5

3

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"
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can always use the infamous brute-force "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"
}

Comments

0

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:

Comments

0

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
 )

Comments

0

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;

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.