| Current Path : /home/jeromecohp/www/tmp/yooessentials/modules/core/src/Util/ |
| Current File : /home/jeromecohp/www/tmp/yooessentials/modules/core/src/Util/Arr.php |
<?php
/**
* @package Essentials YOOtheme Pro (Freemium) 1.6.8
* @author ZOOlanders https://www.zoolanders.com
* @copyright Copyright (C) Joolanders, SL
* @license http://www.gnu.org/licenses/gpl.html GNU/GPL
*/
namespace ZOOlanders\YOOessentials\Util;
class Arr extends \YOOtheme\Arr
{
/**
* Index an array by a key
*
* @param array|\ArrayAccess $array
* @param string|callable $key
*
* @return array
*/
public static function keyBy(array $array, $keyBy): array
{
$results = [];
foreach ($array as $item) {
$resolvedKey = self::resolveKey($item, $keyBy);
if (is_null($resolvedKey)) {
continue;
}
if (is_object($resolvedKey)) {
$resolvedKey = (string) $resolvedKey;
}
$results[$resolvedKey] = $item;
}
return $results;
}
private static function resolveKey($item, $key)
{
if (is_callable($key)) {
return $key($item);
}
if (is_array($item)) {
return $item[$key] ?? null;
}
if (is_object($item)) {
return $item->{$key} ?? null;
}
return null;
}
}