简体   繁体   English

PHP 中的多维数组合并

[英]Multi dimensional Array merge in PHP

Request your help, on how to merge multi dimensional associative array as below, tried the below function, and each one is giving different wrong output and the required output is as below ("Required Array). And Key "[Server]" name remains the same for all the arrays but with different values. Request your help, on how to merge multi dimensional associative array as below, tried the below function, and each one is giving different wrong output and the required output is as below ("Required Array). And Key "[Server]" name remains所有 arrays 相同,但值不同。

Functions功能

$output = array_merge(Array1,Array2,Array3);
$output = array_merge_recursive(Array1,Array2,Array3);
$output = array_replace_recursive(Array1,Array2,Array3);

foreach($Array1 as $a) { $output[$a["Server"]][] = $Count; }
foreach($Array2 as $b) { $output[$b["Server"]][] = $Rack; }
foreach($Array3 as $b) { $output[$b["Server"]][] = $Node; }

Array1:阵列1:

Array
(
    [0] => Array
        (
            [Server] => Server1
            [Count] => 1
        )

    [1] => Array
        (
            [Server] => Server2
            [Count] => 4
        )

    [2] => Array
        (
            [Server] => Server3
            [Count] => 4
        )
)

Array2:数组2:

Array
(
    [0] => Array
        (
            [Server] => Server2
            [Rack] => 1
        )

    [1] => Array
        (
            [Server] => Server4
            [Rack] => 4
        )

    [2] => Array
        (
            [Server] => Server5
            [Rack] => 4
        )
)

Array3:阵列3:

Array
(
    [0] => Array
        (
            [Server] => Server2
            [Node] => 1
        )

    [1] => Array
        (
            [Server] => Server5
            [Node] => 4
        )

    [2] => Array
        (
            [Server] => Server6
            [Node] => 4
        )
)

Required Array Output所需阵列 Output

Array
(
    [0] => Array
        (
            [Server] => Server1
            [Count] => 1
            [Rack] => 0
            [Node] => 0
        )
    [1] => Array
        (
            [Server] => Server2
            [Count] => 4
            [Rack] => 1
            [Node] => 1
        )

    [2] => Array
        (
            [Server] => Server3
            [Count] => 4
            [Rack] => 0
            [Node] => 0
        )

    [3] => Array
        (
            [Server] => Server4
            [Count] => 0
            [Rack] => 4
            [Node] => 0
        )

    [4] => Array
        (
            [Server] => Server5
            [Count] => 0
            [Rack] => 4
            [Node] => 4
        )

    [5] => Array
        (
            [Server] => Server6
            [Count] => 0
            [Rack] => 0
            [Node] => 4
        )
)

From, Vino从,维诺

You can index them by Server and then they will merge into the same key:您可以通过Server对它们进行索引,然后它们将合并到同一个键中:

$output = array_merge(array_column($array1, null, 'Server'),
                      array_column($array2, null, 'Server'),
                      array_column($array3, null, 'Server'));

Not necessary, but if you want to get back to integer keys then:没有必要,但如果您想返回 integer 密钥,则:

$output = array_values($output);

To add missing keys you'll have to merge into a template array:要添加缺少的键,您必须合并到模板数组中:

$keys = ['Server' => 0, 'Count' => 0, 'Rack' => 0, 'Node' => 0];

foreach($output as &$values) {
    $values = array_merge($keys, $values);
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM