Created
February 5, 2014 13:35
-
-
Save tomnomnom/8823701 to your computer and use it in GitHub Desktop.
PHP Extract Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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