Potsky\LaravelLocalizationHelpers\Factory\Tools::arraySetDotFirstLevel PHP Метод

arraySetDotFirstLevel() публичный статический Метод

If no key is given to the method, the entire array will be replaced.
public static arraySetDotFirstLevel ( array &$array, string $key, mixed $value ) : array
$array array
$key string
$value mixed
Результат array
    public static function arraySetDotFirstLevel(&$array, $key, $value)
    {
        if (is_null($key)) {
            return $array = $value;
        }
        $keys = explode('.', $key, 2);
        while (count($keys) > 1) {
            $key = array_shift($keys);
            // If the key doesn't exist at this depth, we will just create an empty array
            // to hold the next value, allowing us to create the arrays to hold final
            // values at the correct depth. Then we'll keep digging into the array.
            if (!isset($array[$key]) || !is_array($array[$key])) {
                $array[$key] = array();
            }
            $array =& $array[$key];
        }
        $array[array_shift($keys)] = $value;
        return $array;
    }

Usage Example

 public function testArraySetDotFirstLevel()
 {
     $array = array();
     Tools::arraySetDotFirstLevel($array, 'a.b.c', true);
     $this->assertTrue($array['a']['b.c']);
     Tools::arraySetDotFirstLevel($array, 'a.b.c', true);
     $this->assertNull(@$array['a']['b']['c']);
     Tools::arraySetDotFirstLevel($array, null, true);
     /** @var bool $array */
     $this->assertTrue($array);
 }
All Usage Examples Of Potsky\LaravelLocalizationHelpers\Factory\Tools::arraySetDotFirstLevel