Kraken\Util\Support\ArraySupport::get PHP Method

get() public static method

Return the value stored under given key in the array with dot notation support.
public static get ( array $array, string $key, mixed $default = null ) : mixed
$array array
$key string
$default mixed
return mixed
    public static function get($array, $key, $default = null)
    {
        $key = static::normalizeKey($key);
        if ($key === null || $key === '') {
            return $array;
        }
        $keys = explode('.', $key);
        $currentElement = $array;
        foreach ($keys as $currentKey) {
            if (!is_array($currentElement) || !array_key_exists($currentKey, $currentElement)) {
                return $default;
            }
            $currentElement = $currentElement[(string) $currentKey];
        }
        return $currentElement;
    }

Usage Example

示例#1
0
 /**
  * @override
  * @inheritDoc
  */
 public function get($key = '', $default = null)
 {
     return $key === '' ? $this->getAll() : ArraySupport::get($this->config, $key, $default);
 }