Skip to content

Instantly share code, notes, and snippets.

@danilchenko
Last active October 19, 2018 19:39
Show Gist options
  • Select an option

  • Save danilchenko/e8f5a3ddf04cde4470e38efd24e53798 to your computer and use it in GitHub Desktop.

Select an option

Save danilchenko/e8f5a3ddf04cde4470e38efd24e53798 to your computer and use it in GitHub Desktop.
entityColumn
/**
* Analogy of array_column function but it's only for entities.
*
* @param array $array
* @param string|null $column
* @param string|null $indexKey
* @param bool $isMultiple
* @return array
*/
public static function entityColumn(
array $array,
string $column = null,
string $indexKey = null,
bool $isMultiple = false
) : array {
foreach ($array as $key => $item) {
$resultKey = $indexKey ? $item->{'get' . $indexKey}() : $key;
$resultValue = $column ? $item->{'get' . $column}() : $item;
if ($isMultiple) {
$result[$resultKey][] = $resultValue;
} else {
$result[$resultKey] = $resultValue;
}
}
return $result ?? [];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment