public static function setPath(&$data, $path, $value)
    {
        $queue = explode('/', $path);
        // Optimization for simple sets.
        if (count($queue) === 1) {
            $data[$path] = $value;
            return;
        }

        $current =& $data;
        while (null !== ($key = array_shift($queue))) {
            if (!is_array($current)) {
                throw new \RuntimeException("Trying to setPath {$path}, but "
                    . "{$key} is set and is not an array");
            } elseif (!$queue) {
                if ($key == '[]') {
                    $current[] = $value;
                } else {
                    $current[$key] = $value;
                }
            } elseif (isset($current[$key])) {
                $current =& $current[$key];
            } else {
                $current[$key] = [];
                $current =& $current[$key];
            }
        }
    }
public static function setPath(&$data, $path, $value)
    {
        $queue = explode('/', $path);
        // Optimization for simple sets.
        if (count($queue) === 1) {
            $data[$path] = $value;
            return;
        }

        $current =& $data;
        while (null !== ($key = array_shift($queue))) {
            if (!is_array($current)) {
                throw new \RuntimeException("Trying to setPath {$path}, but "
                    . "{$key} is set and is not an array");
            } elseif (!$queue) {
                if ($key == '[]') {
                    $current[] = $value;
                } else {
                    $current[$key] = $value;
                }
            } elseif (isset($current[$key])) {
                $current =& $current[$key];
            } else {
                $current[$key] = [];
                $current =& $current[$key];
            }
        }
    }
public static function setPath ( & $ data , $ path , $ value ) { $ queue = explode ( '/' , $ path ) ; // Optimization for simple sets. if ( count ( $ queue ) === 1 ) { $ data [ $ path ] = $ value ; return ; } $ current = & $ data ; while ( null !== ( $ key = array_shift ( $ queue ) ) ) { if ( ! is_array ( $ current ) ) { throw new \ RuntimeException ( "Trying to setPath {$path}, but " . "{$key} is set and is not an array" ) ; } elseif ( ! $ queue ) { if ( $ key == '[]' ) { $ current [ ] = $ value ; } else { $ current [ $ key ] = $ value ; } } elseif ( isset ( $ current [ $ key ] ) ) { $ current = & $ current [ $ key ] ; } else { $ current [ $ key ] = [ ] ; $ current = & $ current [ $ key ] ; } } }
Set a value in a nested array key. Keys will be created as needed to set
the value.

This function does not support keys that contain "/" or "[]" characters
because these are special tokens used when traversing the data structure.
A value may be prepended to an existing array by using "[]" as the final
key of a path.

GuzzleHttp1\get_path($data, 'foo/baz'); // null
GuzzleHttp1\set_path($data, 'foo/baz/[]', 'a');
GuzzleHttp1\set_path($data, 'foo/baz/[]', 'b');
GuzzleHttp1\get_path($data, 'foo/baz');
// Returns ['a', 'b']

@param array  $data  Data to modify by reference
@param string $path  Path to set
@param mixed  $value Value to set at the key

@throws \RuntimeException when trying to setPath using a nested path
that travels through a scalar value.
Set a value in a nested array key . Keys will be created as needed to set the value .