Last active
October 19, 2018 19:39
-
-
Save danilchenko/e8f5a3ddf04cde4470e38efd24e53798 to your computer and use it in GitHub Desktop.
entityColumn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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