PHP使用索引数组作为引用将两个数组合并到一起 - 两个数组作为输出

PHP使用索引数组作为引用将两个数组合并到一起 - 两个数组作为输出

问题描述:

I am trying to take two arrays, and merge them to each other. The first array serves as an 'index' array, that is - that is the format that the output arrays desirably would be:

$array1 = [
    'DIV1' => 'Some element data',
    'SUPPLEMENTAL' => [
        'RPC' => '10.24.122.32',
        'PORT' => '8080'
    ],
    'ASG' =>  'some arbitrary data'
];

$array2 = [
    'DIV2' => 'Some more element data',
    'ASG'  => 'different arbitrary data',
    'DIV1' => 'Some element data that refers to the other object'
    'SUPPLEMENTAL' => [
         'RPC' => '10.24.123.1'
    ]
];

So after the merge, we would effectively have two arrays. This can be done as a a single function called twice, which passes each array as parameters (reversed for the second call - and somehow defining the index array). The keys would be carried over -only-, no values. We would end up with arrays looking like this:

$array1 = [
    'DIV1' => 'Some element data', 
    'DIV2' => '',                       // blank because only key was moved
    'SUPPLEMENTAL' => [
        'RPC' => '10.24.122.32',
        'PORT' => '8080'
    ],
    'ASG' =>  'some arbitrary data'
];

$array2 = [
    'DIV1' => 'Some element data that refers to the other object'
    'DIV2' => 'Some more element data',
    'SUPPLEMENTAL' => [
         'RPC' => '10.24.123.1',
         'PORT' => ''                   // blank because only key was moved
    ],
    'ASG'  => 'different arbitrary data'
];

It is not -extremely- important that the imported (blank) keys are put in some order, but the preservation of order of existing elements is important. As long as it abides by the index arrays order definition (array1 in this case).

I think I would need to do some sort of nested sort for the multiple dimensions.

我正在尝试获取两个数组,并将它们相互合并。 第一个数组用作'索引'数组,即 - 输出数组理想的格式: p>

  $ array1 = [
'DIV1'=  >  '某些元素数据',
'SUPPLEMENTAL'=>  [
'RPC'=>  '10 .24.122.32',
'PORT'=>  '8080'
],
'ASG'=>  '某些任意数据'
]; 
 
 $ array2 = [
'DIV2'=>  '更多元素数据',
'ASG'=>  '不同的任意数据',
'DIV1'=>  '一些引用另一个对象的元素数据'
'SUPPLEMENTAL'=>  [
'RPC'=>  '10 .24.123.1'
] 
]; 
  code>  pre> 
 
 

因此,在合并之后,我们将有效地拥有两个数组。 这可以作为一个调用两次的单个函数来完成,它将每个数组作为参数传递(对于第二个调用反向 - 并以某种方式定义索引数组)。 钥匙只能进行 - 没有价值。 我们最终会得到如下所示的数组: p>

  $ array1 = [
'DIV1'=>  '某些元素数据',
'DIV2'=>  '',//空白,因为只移动了键
'SUPPLEMENTAL'=>  [
'RPC'=>  '10 .24.122.32',
'PORT'=>  '8080'
],
'ASG'=>  '某些任意数据'
]; 
 
 $ array2 = [
'DIV1'=>  '某些元素数据引用另一个对象'
'DIV2'=>  '更多元素数据',
'SUPPLEMENTAL'=>  [
'RPC'=>  '10 .24.123.1',
'PORT'=>  ''//空白,因为只移动了键
],
'ASG'=>  '不同的任意数据'
]; 
  code>  pre> 
 
 

导入的(空白)键按某种顺序放置并不是非常重要,但保存 现有要素的顺序很重要。 只要它遵守索引数组顺序定义(在这种情况下是array1)。 p>

我想我需要对多维进行某种嵌套排序。 p> div>

Because your data doesn't have keys in the same order it'll be difficult to maintain key order, but you can achieve what you need with a recursive function:

function recursiveReKeyArrays(array $array1, array $array2)
{
    // Loop through the array for recursion
    foreach ($array2 as $key => $value) {
        if (!is_array($value)) {
            continue;
        }

        $array1[$key] = recursiveReKeyArrays($array1[$key], $value);
    }

    // Find the differences in the keys
    foreach (array_diff_key($array2, $array1) as $key => $value) {
        $array1[$key] = null;
    }

    return $array1;
}

This will loop through the second array, find any values which are arrays and recurse into them and find any missing keys and set them to null.

This will give you this output:

Array
(
    [DIV1] => Some element data
    [SUPPLEMENTAL] => Array
        (
            [RPC] => 10.24.122.32
            [PORT] => 8080
        )

    [ASG] => some arbitrary data
    [DIV2] => 
)
Array
(
    [DIV2] => Some more element data
    [ASG] => different arbitrary data
    [DIV1] => Some element data that refers to the other object
    [SUPPLEMENTAL] => Array
        (
            [RPC] => 10.24.123.1
            [PORT] => 
        )

)

Example here: http://ideone.com/5ml1y4