1
これらの配列を組み合わせるのは苦労します。設定ファイルから配列を結合する
アプリケーション内のどこでも呼び出すことができるように、複数の構成ファイルを読み込んで1つの大きな配列を作成できます。
test_config1.php
$config1=array();
$config1['Key1']['AnotherKey1']='Value 1';
$config1['Key2']['AnotherKey1']='Value 2';
$config1['Key3']['AnotherKey1']='Value 3';
return $config1;
test_config2.php
$config2=array();
$config2['Different Key 1']='Different Value 1';
$config2['Different Key 2']='Different Value 2';
$config2['Different Key 3']='Different Value 3';
$config2['Key3']['AnotherKey1']['test']='Test 1';
return $config2;
Configureクラス:
class Configure
{
public static $configArray = array();
public static function loadConfig($configSource)
{
# Explicitly turn this into an array.
$configSource=(array)$configSource;
# Loop through the array.
foreach($configSource as $configFileName)
{
$config=require_once $configFileName;
self::$configArray[]=$config;
unset($config);
}
return self::$configArray;
}
}
print_r(Configure::loadConfig(array('test_config1.php', 'test_config2.php')));
//print_r(Configure::loadConfig('test_config1.php'));
結果:
Array
(
[0] => Array
(
[Key1] => Array
(
[AnotherKey1] => Value 1
)
[Key2] => Array
(
[AnotherKey1] => Value 2
)
[Key3] => Array
(
[AnotherKey1] => Value 3
)
)
[1] => Array
(
[Different Key 1] => Different Value 1
[Different Key 2] => Different Value 2
[Different Key 3] => Different Value 3
[Key3] => Array
(
[AnotherKey1] => Array
(
[test] => Test 1
)
)
)
)
募集:
[Key3] => Array
(
[AnotherKey2] => Array
(
[test] => Test 1
)
)
そして配列から$config2['Key3']['AnotherKey2']['test']='Test 1';
を抜けてself::$configArray=self::$configArray + $config;
を:与える私はself::$configArray=array_merge(self::$configArray, $config);
を試してみました
Array
(
[Key1] => Array
(
[AnotherKey1] => Value 1
)
[Key2] => Array
(
[AnotherKey1] => Value 2
)
[Key3] => Array
(
[AnotherKey1] => Value 3
[AnotherKey2] => Array
(
[test] => Test 1
)
)
[Different Key 1] => Different Value 1
[Different Key 2] => Different Value 2
[Different Key 3] => Different Value 3
)
。
おかげで、それは(array_merge 'と同じことをし)' – Draven
今、私はしましたあなたは複数の似た鍵を持っているのを見ました...まあ、私は自分の答えを更新しました。 –
ああ、 'array_merge_recursive()'、右に。以前はその機能を使用していませんでした。作品。 – Draven