merge() public static method

If each array has an element with the same string key value, the latter will overwrite the former (different from array_merge_recursive). Recursive merging will be conducted if both arrays have an element of array type and are having the same key. For integer-keyed elements, the elements from the latter array will be appended to the former array.
public static merge ( array $a, array $b ) : array
$a array array to be merged to
$b array array to be merged from. You can specifiy additional arrays via third argument, fourth argument etc.
return array the merged array (the original arrays are not changed.)
Ejemplo n.º 1
0
 /**
  * Reads the configuration settings from the file
  * @return array|mixed
  * @throws \Exception
  */
 public static function settings()
 {
     if (null === self::$_settings) {
         self::$_settings = file_exists(self::getConfigurationDirectoryPath() . '/settings.php') ? require_once self::getConfigurationDirectoryPath() . '/settings.php' : array();
         self::$_settings['envlock'] = file_exists(self::getEnvironmentLockFilePath());
         if (($customSettings = ArrayX::get(self::$_settings, 'custom.path')) !== null && file_exists($customSettings)) {
             self::$_settings = ArrayX::merge(self::$_settings, require_once $customSettings);
         }
     }
     if (empty(self::$_settings)) {
         throw new \Exception('Unable to find Yiinitialzr settings file!');
     }
     return self::$_settings;
 }
Ejemplo n.º 2
0
 /**
  * @param $directory
  * @param $files  array of configuration files to merge
  * @return array
  */
 public static function build($directory, $files)
 {
     $result = array();
     if (!is_array($files)) {
         $files = array($files);
     }
     foreach ($files as $file) {
         $config = file_exists($file) && is_file($file) ? require $file : (is_string($file) && file_exists($directory . '/' . $file . '.php') ? require $directory . '/' . $file . '.php' : array());
         if (is_array($config)) {
             $result = ArrayX::merge($result, $config);
         }
     }
     return $result;
 }