PHP - 在多维数组中合并/组合数组值

时间:2013-08-27 09:48:49

标签: php arrays multidimensional-array merge

我怎么能转换这个数组:

$a = array(
 0 => array(
     0 => 31,
     1 => 39
 ),
 1 => array(
     0 => 41
 )
);

到这一个:

   $a = array(
    0 => array(
        0 => array(
            0 => 31,
            1 => 41
        ),
        1 => array(
            0 => 39,
            1 => 41
        )
    ),
    1 => array(
        0 => array(
            0 => 41,
            1 => 31
        ),
        1 => array(
            0 => 41,
            1 => 39
        )
    )
);

我尝试了几种方法,但找不到合适的解决方案。目前我的大脑过热了。所以也许有人有我的解决方案。

谢谢@Manos。

不幸的是有动态数组。所以这些静态功能对我不起作用。

所以数组也可能如下所示:

$a = array(
 0 => array(
     0 => 31,
     1 => 39
 ),
 1 => array(
     0 => 41,
     1 => 49,
     2 => 51
 ),
 2 => array(
     0 => 73
 )
);

输出应如下所示:

   $a = array(
        0 => array(
            0 => array(
                0 => 31,
                1 => 41,
                2 => 73
            ),
            1 => array(
                0 => 31,
                1 => 49,
                2 => 73
            ),
            2 => array(
                0 => 31,
                1 => 51,
                2 => 73
            ),
            3 => array(
                0 => 39,
                1 => 41,
                2 => 73
            ),
            4 => array(
                0 => 39,
                1 => 49,
                2 => 73
            ),
            5 => array(
                0 => 39,
                1 => 51,
                2 => 73
        ),
        1 => array(
            0 => array(
                0 => 41,
                1 => 31,
                2 => 73
            ),
            1 => array(
                0 => 41,
                1 => 39,
                2 => 73
            ),
            2 => array(
                0 => 49,
                1 => 31,
                2 => 73
            ),
            3 => array(
                0 => 49,
                1 => 39,
                2 => 73
            ),
                etc ......
        )
    );

马诺斯函数输出:

    array(3) {
  [0]=>
  array(8) {
    [0]=>
    array(2) {
      [0]=>
      int(31)
      [1]=>
      int(41)
    }
    [1]=>
    array(2) {
      [0]=>
      int(39)
      [1]=>
      int(41)
    }
    [2]=>
    array(2) {
      [0]=>
      int(31)
      [1]=>
      int(49)
    }
    [3]=>
    array(2) {
      [0]=>
      int(39)
      [1]=>
      int(49)
    }
    [4]=>
    array(2) {
      [0]=>
      int(31)
      [1]=>
      int(51)
    }
    [5]=>
    array(2) {
      [0]=>
      int(39)
      [1]=>
      int(51)
    }
    [6]=>
    array(2) {
      [0]=>
      int(31)
      [1]=>
      int(73)
    }
    [7]=>
    array(2) {
      [0]=>
      int(39)
      [1]=>
      int(73)
    }
  }
  [1]=>
  array(9) {
    [0]=>
    array(2) {
      [0]=>
      int(41)
      [1]=>
      int(31)
    }
    [1]=>
    array(2) {
      [0]=>
      int(49)
      [1]=>
      int(31)
    }
    [2]=>
    array(2) {
      [0]=>
      int(51)
      [1]=>
      int(31)
    }
    [3]=>
    array(2) {
      [0]=>
      int(41)
      [1]=>
      int(39)
    }
    [4]=>
    array(2) {
      [0]=>
      int(49)
      [1]=>
      int(39)
    }
    [5]=>
    array(2) {
      [0]=>
      int(51)
      [1]=>
      int(39)
    }
    [6]=>
    array(2) {
      [0]=>
      int(41)
      [1]=>
      int(73)
    }
    [7]=>
    array(2) {
      [0]=>
      int(49)
      [1]=>
      int(73)
    }
    [8]=>
    array(2) {
      [0]=>
      int(51)
      [1]=>
      int(73)
    }
  }
  [2]=>
  array(5) {
    [0]=>
    array(2) {
      [0]=>
      int(73)
      [1]=>
      int(31)
    }
    [1]=>
    array(2) {
      [0]=>
      int(73)
      [1]=>
      int(39)
    }
    [2]=>
    array(2) {
      [0]=>
      int(73)
      [1]=>
      int(41)
    }
    [3]=>
    array(2) {
      [0]=>
      int(73)
      [1]=>
      int(49)
    }
    [4]=>
    array(2) {
      [0]=>
      int(73)
      [1]=>
      int(51)
    }
  }
}

1 个答案:

答案 0 :(得分:1)

foreach ($a as $first_group_key => $first_group) {
    foreach ($a as $second_group_key => $second_group) {
        if ($second_group_key == $first_group_key) {
            continue;
        }
        $i = count($b[$first_group_key]);
        foreach ($second_group as $second_value) {
            foreach ($first_group as $first_key => $first_value) {
                $b[$first_group_key][$i][0] = $first_value;
                $b[$first_group_key][$i][1] = $second_value;
                $i++;
            }
        }
    }
}