21

I have an array that has countries:

array(
    'AF' => 'AFGHANISTAN',
    'AL' => 'ALBANIA',
    'DZ' => 'ALGERIA',
    'AS' => 'AMERICAN SAMOA',
);

and I have another array that has some of the keys in it

array('AL', 'DZ');

I want to call a function that will take both arrays as parameters and return

array(
    'AL' => 'ALBANIA',
    'DZ' => 'ALGERIA',
);

I know php has built in functions to compare the keys, or the values, but it seems those functions all expect you to have two 1D arrays or two 2D arrays.

I could loop over array_keys() for the first array and do a in_array() check on each key, but that seems really inefficient.

0

4 Answers 4

49
$selection = array('AL', 'DZ');
$filtered = array_intersect_key($countries, array_flip($selection));
var_dump($filtered);
Sign up to request clarification or add additional context in comments.

4 Comments

I had to look twice, but it is a very nice, short solution. +1
@deceze - good solution - is there an efficient way to calculate if ALL values in the new array match (for example here, a check: $selection = array('AL','AF','DZ','AS');
@JM4 Yes, do a diff instead of an intersect.
@deceze - thanks for the help. FWIW I ended up having to go a different route because I also wanted to test if new values appeared in $selection as diff. I just used the initial code above then ran a count() of the values on $selection and $filtered to determine if the values matched.
4

Simply loop over the SECOND array, and fetch the values from the first. Vise versa seems unneeded inefficient indeed.

So:

$Arr1 = array(
'AF'=>'AFGHANISTAN',
'AL'=>'ALBANIA',
'DZ'=>'ALGERIA',
'AS'=>'AMERICAN SAMOA',
);

$Arr2 = array('AL', 'DZ');

$result = array();
foreach ($Arr2 as $cc){
  if (isset($Arr1[$cc])){
    $result[$cc] = $Arr1[$cc];
  }
}
print_r($result);

I don't think that is inefficient.

Edit addition: If you are 100% sure the $Arr2 contains only codes that can be found in $Arr1, you can of course skip the isset() test.

2 Comments

+1 for efficiency, though I prefer my intersection approach as more concise and probably efficient enough... :)
@deceze: Personally I need more time to understand your approach. While it is shorter (lines of code) I must think twice about it. How it handles missing entries for example. After thinking twice I see it handles them just alright. ;-) Nice solution!
-1

I think this will help. Here is a function key_values_intersect that will work as you expected :)

$longcodes = array(
    'AF' => 'AFGHANISTAN',
    'AL' => 'ALBANIA',
    'DZ' => 'ALGERIA',
    'AS' => 'AMERICAN SAMOA',
);

$code = array('AL', 'DZ');

function key_values_intersect($haystack, $needle)
{
    $tmp=array();
    foreach ($needle AS $key) {
        $tmp[$key] = $haystack[$key];
    }
    return $tmp;
}


print_r(key_values_intersect($longcodes,$code));

Comments

-2

If I understood correctly You have array of countries and array of needed countries and You want to create array with needed countries. If that, then You can try this:

$countries = array ("AF"=>"AFGJANISTAN", "AL"=>"ALBANIA", "LV"=>"LATVIA", "USA"=>"UNITED STATES OF AMERICA");
$needed  = array ("AF", "AL");

$result = array ();
foreach ($needed as $row) {
   if (in_array($row, $contries)) {
       $result[] = $countries[$row];
   }
}
var_dump($result);

1 Comment

This is incorrect answer. Please, read how in_array function works at php.net/manual/en/function.in-array.php

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.