Kraken\Util\Support\ArraySupport::set PHP Méthode

set() public static méthode

Set the value for given key in the array with dot notation support.
public static set ( &$array, string $key, mixed $value ) : array
$key string
$value mixed
Résultat array
    public static function set(&$array, $key, $value)
    {
        $key = static::normalizeKey($key);
        if ($key === null || $key === '') {
            return $array = $value;
        }
        $keys = explode('.', $key);
        $last = array_pop($keys);
        $currentElement =& $array;
        foreach ($keys as $currentKey) {
            if (!array_key_exists($currentKey, $currentElement) || !is_array($currentElement[$currentKey])) {
                $currentElement[$currentKey] = [];
            }
            $currentElement =& $currentElement[$currentKey];
        }
        $currentElement[$last] = $value;
        return $array;
    }

Usage Example

Exemple #1
0
 /**
  * @override
  * @inheritDoc
  */
 public function set($key, $value)
 {
     return ArraySupport::set($this->config, $key, $value);
 }