0

我怎么能转换这个数组:

$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 ......
        )
    );

Manos 函数输出:

    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)
    }
  }
}
4

1 回答 1

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++;
            }
        }
    }
}
于 2013-08-27T10:05:16.843 回答