0

Is there a way to do the following:

$array1 = array( "two" => "2", "three" => "3")

$array2 = array("two", "three", "four")

I want to match array2's value with array1's key. Upon matching, I want to output array1's value.

Thank you

4
  • 2
    Please provide code showing what you have tried, and the errors/problems you encountered. Commented Jul 30, 2013 at 10:48
  • 1
    And your expected output also? Commented Jul 30, 2013 at 10:48
  • 2
    It should be a simple loop over array2's values. Commented Jul 30, 2013 at 10:48
  • 2
    array_intersect_key() could be a useful function here; perhaps used with array_flip() on your second array Commented Jul 30, 2013 at 10:53

2 Answers 2

3

Like Mark Baker commented, you can use array_flip() together with array_intersect_key()

$array1 = array( "two" => "2", "three" => "3");

$array2 = array("two", "three", "four");
$array2 = array_flip($array2);

print_r(array_intersect_key($array1, $array2) );

Output:

Array
(
    [two] => 2
    [three] => 3
)
Sign up to request clarification or add additional context in comments.

Comments

1
$array1 = array( "two" => "2", "three" => "3");
foreach($array1 as $key=>$val){
    $array_1[] = $key;
}
$array2 = array("two", "three", "four");


$result = array_diff($array2, $array_1);

print_r($result);

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.