php 数组的合并的算法问题?

lawlietee
  • 35

现在有两个二维数组,第一个数组中子元素下包含着,第二个二维数组的索引,现在想将数组合并成第一个数组直接对应第二个数组下的子元素
例:

array1 => array(
'key1' => array(
        0 => 'a',
        1 => 'b',
    ),
 'key2'=> array(
        0 => 'd',
        1 => 'e',
        2 => 'f'
 )   
 。。。。
)

array2 => array(
    'a' =>array(
        0 => 1,
        1 => 2 ,
        2 => 3,
    ),
    'b' => array(
        0 => 4,
        1 => 5 ,
        2 => 6,
    ),
    
   。。。。
 
    'f' => array(
         0 => 7,
        1 => 8 ,
        2 => 9,
    )
)

希望能够得到的输出结果是

array(
'key1' => array(
        0 => 1,
        1 => 2 ,
        2 => 3,
        3 => 4,
        4 => 5 ,
        5 => 6,
)
'key2' => array(
        ....
)
....
)

   即 数组1的键 => array( 包含数组二的索引)
   数组2的键  => array( 是数组1 每个元素的子元素)

求:

  数组1的键 => array(对应的数组2下所有的元素)
回复
阅读 1.8k
4 个回答
✓ 已被采纳

如果不限制 O(n),直接将数组2的值 merger 到数组1的 key 就 ok 了,不需要执行楼上 @苏生不惑 array_keys 和 array_values这两步

<?php

$a = [
    'key1' => [
        0 => 'a',
        1 => 'b',
    ],
    'key2' => [
        0 => 'd',
        1 => 'e',
        2 => 'f',
    ],
];
$b = [
    'a' => [
        0 => 1,
        1 => 2,
        2 => 3,
    ],
    'b' => [
        0 => 4,
        1 => 5,
        2 => 6,
    ],
    'f' => [
        0 => 7,
        1 => 8,
        2 => 9,
    ],
];
$r = [];
foreach ($a as $k => $val) {
    $r[$k] = [];
    foreach ($val as $key2) {
       isset($b[$key2]) && $r[$k] = array_merge($r[$k], $b[$key2]);
    }
}
print_r($r);
/*
Array
(
    [key1] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
        )

    [key2] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 9
        )

)
*/
$a = array(
        'key1' => array(
                0 => 'a',
                1 => 'b',
            ),
         'key2'=> array(
                0 => 'd',
                1 => 'e',
                2 => 'f'
         )   
        );

        $b = array(
            'a' =>array(
                0 => 1,
                1 => 2 ,
                2 => 3,
            ),
            'b' => array(
                0 => 4,
                1 => 5 ,
                2 => 6,
            ),
            
          
         
            'f' => array(
                 0 => 7,
                1 => 8 ,
                2 => 9,
            )
        );
        $k = array_keys($a);
        $r=  [];
        foreach ($k as $key => $value) {
            $v = array_values($a[$value]);
            foreach ($v as $keys => $values) {
                if (isset($b[$values])) {
                    !isset($r[$value]) && $r[$value]=[];
                    $r[$value] = array_merge($r[$value],$b[$values]);
                }
            }
        }
    print_r($r);    
    
    /*
    
    Array
(
    [key1] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
        )

    [key2] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 9
        )

)*/
        

循环数组,然后重组成一个新的数组。

最基础的写法了:
foreach ($arrayOne as $k => $v) {
    foreach ($v as $key => $value) {
        $arrayOne[$k][$key] = $arrayTwo[$value];
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏