4
\$\begingroup\$

I have an associative array with data (let's say language codes and descriptions) and a second array with allowed keys (lang codes). I want to filter the data array by these allowed keys.

The problem is I'm bound to PHP 5.5 and I can't use ARRAY_FILTER_USE_KEY flag.

I came up with the following solution:

$langs = [
 'en' => "English",
 'de' => "German",
 'fr' => "French",
 'ru' => "Russian",
];

$allowed_langs = ['en','de'];

var_export( 
array_map( function($lang) use($langs) {return $langs[$lang];} , array_combine($allowed_langs, $allowed_langs))
);

/* Output as expected:
array (
  'en' => 'English',
  'de' => 'German',
)
*/

I wonder is there a more elegant and shorter solution to this task?

\$\endgroup\$

2 Answers 2

7
\$\begingroup\$

I always believe that you should use built in functions wherever possible, as opposed to recreating PHP functionality with loops etc. The main reasons for saying this are that:

  1. We should trust that PHP functions achieve their desired result in an efficient manner, and:
  2. If they are improved in future versions your code doesn't need to change, but just gets better.

That being said, why not something like this:

$matches = array_intersect_key($langs, array_flip($allowed_langs));
var_export($matches);
\$\endgroup\$
2
  • \$\begingroup\$ Yes, indeed! This solution is definitely better than mine. \$\endgroup\$
    – cFreed
    Jul 27, 2016 at 12:41
  • \$\begingroup\$ A similar solution is in the PHP array_filter documentation comments, that uses array_diff_key instead of array_intersect_key \$\endgroup\$
    – icc97
    Sep 11, 2017 at 16:18
3
\$\begingroup\$

I find your solution rather complicated, so I would prefer this one:

foreach ($langs as $k => $v) {
  if (in_array($k, $allowed_langs)) {
    $result[$k] = $v;
  }
}
var_export($result);

I' m incertain of which one is faster (and I tend to think this shouldn't be really important here), but I'm sure it's more simple and readable.

\$\endgroup\$
7
  • \$\begingroup\$ Small comment: - "AS" -> "as" (highlighter doesn't highlight the CAPS). \$\endgroup\$
    – Ruslan Bes
    Jul 27, 2016 at 8:21
  • \$\begingroup\$ @RuslanBes Not sure what you address about highlighter: is it the SO's one? Anyway, I'm not initially partisan for this capitalization: I only resigned using it because it seems to be somewhat part of shared best practices, like for TRUE, FALSE, and NULL... and probably some other ones I don't have in mind at this time. \$\endgroup\$
    – cFreed
    Jul 27, 2016 at 12:45
  • 2
    \$\begingroup\$ @cFreed PSR actually recommends not using caps for true, false, null or as - php-fig.org/psr/psr-2 \$\endgroup\$
    – C Ivemy
    Jul 27, 2016 at 16:11
  • \$\begingroup\$ @cFreed it's just my perfectionism. If you write as then the SO highlighter will make it blue like foreach and if. Now it thinks apparently that it's a constant. Upvoted it anyway \$\endgroup\$
    – Ruslan Bes
    Jul 27, 2016 at 20:51
  • \$\begingroup\$ @cFreed as C Ivemy said, it's actually the opposite in terms of "best practice" \$\endgroup\$
    – scrowler
    Jul 27, 2016 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.