title tags
pluck
array,beginner

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);
}
pluck([
  ['product_id' => 'prod-100', 'name' => 'Desk'],
  ['product_id' => 'prod-200', 'name' => 'Chair'],
], 'name');
// ['Desk', 'Chair']

Tags: PHP pluck, PHP arrays, PHP Objects, Array_map, Object Map, Specific Item from Array or Object, Iterate Object, Iterate Array, Search Array, Search Object