dounao1875 2017-06-15 04:47
浏览 65
已采纳

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.

  • 写回答

1条回答 默认 最新

  • doujiufutaog59220 2017-06-15 05:39
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭