GeneralFunctions\pluckProperty()

transformer variadic returns Closure pure
...string → (Record → mixed)
At a glance — Deep getProperty — walks a nested path. Returns null if any step is missing along the way.

Creates a Closure that traverses a nested path of properties/indexes across arrays and objects. Works with any mix of the two — even ArrayAccess objects using array syntax.

/**
  * @param string ...$nodes
  * @return Closure(mixed[]|object):mixed
  */
GeneralFunctions\pluckProperty(string ...$nodes): Closure

Returned Closure

When GeneralFunctions\pluckProperty() is called, it returns the following Closure which can be used like a regular function.

/**
  * @param array|object $record
  * @return mixed
  */
$function ($record)

Examples

Partial Application

This can be used to create a simple closure which can be used as a regular function.

$getAuthorName = GeneralFunctions\pluckProperty('details', 'author', 'name');

$book = [
  'details' => [
    'author' => (object) ['name' => 'Ada'],
  ],
];

echo $getAuthorName($book); // 'Ada'

var_dump($getAuthorName(['details' => []])); // NULL (short path)

Details

General Functions

Releated Property access Functions