Skip to content

Instantly share code, notes, and snippets.

@edwinheij
Created July 23, 2014 23:27
Show Gist options
  • Save edwinheij/f5ffb3d1fd39cc559af1 to your computer and use it in GitHub Desktop.
Save edwinheij/f5ffb3d1fd39cc559af1 to your computer and use it in GitHub Desktop.
<?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