Php 从配置文件组合数组

Php 从配置文件组合数组,php,arrays,Php,Arrays,很难组合这些阵列 我希望能够加载多个配置文件并构建一个大阵列,以便能够调用应用程序中的任何位置 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 $c

很难组合这些阵列

我希望能够加载多个配置文件并构建一个大阵列,以便能够调用应用程序中的任何位置

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;
配置类:

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
                        )

                )

        )

)
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
(
    [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
                        )

                )

        )

)
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
)
我尝试了
self::$configArray=array\u merge(self::$configArray,$config)其中给出:

[Key3] => Array
    (
        [AnotherKey2] => Array
            (
                [test] => Test 1
            )

    ) 
self::$configArray=self::$configArray+$config省略了
$config2['Key3']['AnotherKey2']['test']='test1'来自阵列。

尝试此代码(未测试):

更新多个类似密钥:

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;

        if (empty(self::$configArray)) {
            self::$configArray = $config;
        } else {
            self::$configArray = array_merge_recursive(self::$configArray, $config);
        }
        unset($config);
    }
    return self::$configArray;
}
}

谢谢,但这与
array\u merge()。。。我已经更新了我的答案。啊,
array\u merge\u recursive()
,马上。以前从未使用过该功能。唯一的问题是我不能同时使用两个
Configure::loadConfig()
调用。给出错误:“警告:数组#合并#递归():参数#2不是数组”<代码>$config=require\u once$configFileName
在第二次调用期间返回
布尔值true
。要么将其强制转换为数组,要么检查它是否为数组,如果不是,则将该值添加到新创建的数组中作为第二个参数传递。如果您多次调用loadConfig,为什么它是静态的?