Voodoo\Core\Helpers::setArrayToDotNotation PHP Method

setArrayToDotNotation() public static method

To set value in array with dot notation
public static setArrayToDotNotation ( type &$root, type $dotNotationKeys, type $value )
$root type
$dotNotationKeys type
$value type
    public static function setArrayToDotNotation(&$root, $dotNotationKeys, $value)
    {
        $keys = explode('.', $dotNotationKeys);
        while (count($keys) > 1) {
            $key = array_shift($keys);
            if (!isset($root[$key])) {
                $root[$key] = array();
            }
            $root =& $root[$key];
        }
        $key = reset($keys);
        $root[$key] = $value;
    }

Usage Example

Example #1
0
 /**
  * Assign variable
  * @param  mixed          $key - can be string, dot notation k/v, or array to set data as bulk
  * @param  mixed          $val - can be string, numeric, array
  * @return Api
  */
 public function assign($key, $val = "")
 {
     if (is_string($key) || is_array($key)) {
         $data = array();
         if (is_string($key)) {
             if (preg_match("/\\./", $key)) {
                 // dot notation keys
                 Core\Helpers::setArrayToDotNotation($data, $key, $val);
             } else {
                 $data[$key] = $val;
             }
         } else {
             $data = $key;
         }
         $this->assigned = array_merge_recursive($data, $this->assigned);
         return $this;
     } else {
         throw new Core\Exception("Can't assign() {$key}. Invalid key type. Must be string or array");
     }
 }