src/Collection.php
public function set($keyPath, $value)
    {
        $stops = explode('.', $keyPath);

        $currentLocation = $previousLocation = $this;
        foreach ($stops as $key) {
            if ($currentLocation instanceof Collection) {
                // Move one step deeper into the collection
                if (!($currentLocation->$key instanceof Collection)) {
                    $currentLocation->$key = array();
                }
            } else {
                $currentLocation = array();
                $currentLocation->$key = array();
            }
            $previousLocation = $currentLocation;
            $currentLocation = $currentLocation->$key;
        }

        // Set the value
        $previousLocation->$key = $value;
    }
public function set($keyPath, $value)
    {
        $stops = explode('.', $keyPath);

        $currentLocation = $previousLocation = $this;
        foreach ($stops as $key) {
            if ($currentLocation instanceof Collection) {
                // Move one step deeper into the collection
                if (!($currentLocation->$key instanceof Collection)) {
                    $currentLocation->$key = array();
                }
            } else {
                $currentLocation = array();
                $currentLocation->$key = array();
            }
            $previousLocation = $currentLocation;
            $currentLocation = $currentLocation->$key;
        }

        // Set the value
        $previousLocation->$key = $value;
    }
Set a value to an index in the collection
Used when the collection are nested

@param string $keyPath
@param string $value