array_combine并使用array_combine在此数组中创建新数组

时间:2012-12-23 02:49:00

标签: php arrays multidimensional-array

这就是我现在所拥有的。

Array(
[0] => Array
    (
        [x] => 1
        [y] => 1418
        [z] => 1
    )

[1] => Array
    (
        [x] => 2
        [y] => 567
        [z] => 1
    )

[2] => Array
    (
        [x] => 3
        [y] => 2345
        [z] => 1
    )

[3] => Array
    (
        [x] => 4
        [y] => 567
        [z] => 1
    )

[4] => Array
    (
        [x] => 1
        [y] => 2134
        [z] => 2
    )

[5] => Array
    (
        [x] => 2
        [y] => 456
        [z] => 2
    )

[6] => Array
    (
        [x] => 3
        [y] => 1418
        [z] => 2
    )

[7] => Array
    (
        [x] => 4
        [y] => 676
        [z] => 2
    )
)

我想要:

Array(
[1] => Array
    (
        [1] => Array
             (
                [y] => 1418
             )
        [2] => Array
             (
                [y] => 567
             )
        [3] => Array
             (
                [y] => 2345
             )
        [4] => Array
             (
                [y] => 567
             )
    )
[2] => Array
    (
        [1] => Array
             (
                [y] => 2134
             )
        [2] => Array
             (
                [y] => 456
             )
        [3] => Array
             (
                [y] => 1418
             )
        [4] => Array
             (
                [y] => 676
             )
    )
)

现在,一点解释。我想从这个数组,我创建了一个新的数组,其中: [z] =是第一个数组的关键 [x] =是第二阵列的关键。

[z]和[x]不必从数组中删除。

有人可以帮我怎么做吗?因为策划而不去找我。特别是,我在这里创建一个新数组时遇到了问题。

var_export:

array (
   0 => 
   array (
       'x' => '1',
       'y' => '1418',
       'z' => '1',
   ),
   1 => 
   array (
       'x' => '2',
       'y' => '567',
       'z' => '1',
   ),
   2 => 
   array (
       'x' => '3',
       'y' => '2345',
       'z' => '1',
   ),
   3 => 
   array (
       'x' => '4',
       'y' => '567',
       'z' => '1',
   ),
   4 => 
   array (
       'x' => '1',
       'y' => '2134',
       'z' => '2',
   ),
   5 => 
   array (
       'x' => '2',
       'y' => '456',
       'z' => '2',
   ),
   6 => 
   array (
       'x' => '3',
       'y' => '1418',
       'z' => '2',
   ),
   7 => 
   array (
       'x' => '4',
       'y' => '676',
       'z' => '2',
   ),
)

1 个答案:

答案 0 :(得分:1)

array_chunk正是您要找的。

你想要的是:array_chunk($array, 4)