30 seconds of PHP

  Back to Home

pluck

arraybeginner

Retrieves all of the values for a given key.

Use array_map() to map each object in the $items array to the provided $key.

function pluck($items, $key)
{
  return array_map( function($item) use ($key) {
    return is_object($item) ? $item->$key : $item[$key];
  }, $items);
}