Created
July 23, 2014 23:27
-
-
Save edwinheij/f5ffb3d1fd39cc559af1 to your computer and use it in GitHub Desktop.
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 | |
$peoples = array( | |
array("name" => "jeffrey", "age" => 28), | |
array("name" => "john", "age" => 20) | |
); | |
// the common way | |
function array_pluck($toPluck, $arr) { | |
$ret = array(); | |
foreach($arr as $item) { | |
$ret[] = $item[$toPluck]; | |
} | |
return $ret; | |
} | |
// the fabulous way | |
function array_pluck ($toPluck, $arr) { | |
return array_map(function ($item) use ($toPluck) { | |
return $item[$toPluck]; | |
}, $arr); | |
} | |
print_r(array_pluck("name", $peoples)); // array("jeffrey", "john") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment