Skip to content

Instantly share code, notes, and snippets.

@sobi3ch
Last active October 24, 2017 17:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sobi3ch/5451004 to your computer and use it in GitHub Desktop.
Save sobi3ch/5451004 to your computer and use it in GitHub Desktop.
PHP Array pluck function
<?php
/**
* Pluck an array of values from an array. (Only for PHP 5.3+)
*
* @param $array - data
* @param $key - value you want to pluck from array
*
* @return plucked array only with key data
*/
function array_pluck($array, $key) {
return array_map(function($v) use ($key) {
return is_object($v) ? $v->$key : $v[$key];
}, $array);
}
@sobi3ch
Copy link
Author

sobi3ch commented Apr 24, 2013

@sobi3ch
Copy link
Author

sobi3ch commented May 8, 2013

@LucaRosaldi
Copy link

Isn’t this the same as array_column()?

@moui72
Copy link

moui72 commented Oct 24, 2017

@LucaRosaldi array_column() only works on arrays of objects after PHP 7.0, so this is a useful polyfill for users of PHP > 5.3 && PHP < 7.0

I've just found myself in that situation and used it =) thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment