Skip to content

Instantly share code, notes, and snippets.

@tomnomnom
Created February 5, 2014 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomnomnom/8823701 to your computer and use it in GitHub Desktop.
Save tomnomnom/8823701 to your computer and use it in GitHub Desktop.
PHP Extract Function
<?php
function array_extract(array $input, array $keys){
$output = [];
foreach ($keys as $key){
if (!array_key_exists($key, $input)) continue;
$output[$key] = $input[$key];
}
return $output;
}
$super = [
'one' => 1,
'two' => 2,
'three' => 3
];
$sub = array_extract($super, ['one', 'three']);
var_export($sub);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment