lithium\util\Set::merge PHP Method

merge() public static method

This method can be thought of as a hybrid between PHP's array_merge() and array_merge_recursive(). The difference to the two is that if an array key contains another array then the function behaves recursive (unlike array_merge()) but does not do if for keys containing strings (unlike array_merge_recursive()). Please note: This function will work with an unlimited amount of arguments and typecasts non-array parameters into arrays.
public static merge ( array $array1, array $array2 ) : array
$array1 array The base array.
$array2 array The array to be merged on top of the base array.
return array Merged array of all passed params.
    public static function merge(array $array1, array $array2)
    {
        $args = array($array1, $array2);
        if (!$array1 || !$array2) {
            return $array1 ?: $array2;
        }
        $result = (array) current($args);
        while (($arg = next($args)) !== false) {
            foreach ((array) $arg as $key => $val) {
                if (is_array($val) && isset($result[$key]) && is_array($result[$key])) {
                    $result[$key] = static::merge($result[$key], $val);
                } elseif (is_int($key)) {
                    $result[] = $val;
                } else {
                    $result[$key] = $val;
                }
            }
        }
        return $result;
    }

Usage Example

Beispiel #1
0
 /**
  * Sets up the adapter with the configuration assigned by the `Session` class.
  *
  * @param array $config Available configuration options for this adapter:
  *				- `'config'` _string_: The name of the model that this adapter should use.
  */
 public function __construct(array $config = array())
 {
     $this->config = Set::merge($this->config, $config);
     if (empty($this->config['model']) || !class_exists($this->config['model'])) {
         throw new ConfigException("No valid model \"{$this->config['model']}\" available to use for Session interaction");
     } elseif (empty($this->config['entityManager']) && (!method_exists($this->config['model'], 'getEntityManager') || !is_callable($this->config['model'] . '::getEntityManager'))) {
         throw new ConfigException("The session model {$this->config['model']} must define a getEntityManager() static method, or you must set the entityManager session config variable");
     }
     $reflection = new \ReflectionClass($this->config['model']);
     if (!$reflection->implementsInterface('li3_doctrine2\\models\\ISession')) {
         throw new ConfigException("The model {$this->config['model']} must implement ISession");
     }
     $this->entityManager = $this->config['entityManager'] ?: call_user_func($this->config['model'] . '::getEntityManager');
     if (!isset($this->entityManager) || !$this->entityManager instanceof EntityManager) {
         throw new ConfigException('Not a valid entity manager');
     }
     $this->repository = $this->entityManager->getRepository($this->config['model']);
     foreach ($this->config['ini'] as $key => $config) {
         if (isset($this->config['ini'][$key]) && ini_set("session.{$key}", $this->config['ini'][$key]) === false) {
             throw new ConfigException("Could not initialize the session variable {$key}");
         }
     }
     session_set_save_handler(array(&$this, '_open'), array(&$this, '_close'), array(&$this, '_read'), array(&$this, '_write'), array(&$this, '_destroy'), array(&$this, '_gc'));
     register_shutdown_function('session_write_close');
     if ($this->config['start']) {
         $this->_startup();
     }
 }
All Usage Examples Of lithium\util\Set::merge