2014-02-13 65 views
0

如何合併二維數組的第二個和第三個,第四個和第五個,第六個和第七個...值?第一個數組[數字]不應合併:PHP:在二維數組中合併數組

Array 
(
[number] => Array 
    (
     [0] => 1234 
     [1] => 2345 
     [2] => 3456 
    ) 
[vote01] => Array 
    (
     [0] => 000 
     [1] => 000 
     [2] => 001 
    ) 
[vote02] => Array 
    (
     [0] => 002 
     [1] => 002 
     [2] => 003 
    ) 
[vote03] => Array 
    (
     [0] => 004 
     [1] => 004 
     [2] => 005 
    ) 
[vote04] => Array 
    (
     [0] => 006 
     [1] => 007 
     [2] => 008 
    ) 
    ... 
) 

合併:

Array 
(
[number] => Array 
    (
     [0] => 1234 
     [1] => 2345 
     [2] => 3456 
    ) 
[new01] => Array 
    (
     [0] => 000 
     [1] => 000 
     [2] => 001 
     [3] => 002 
     [4] => 002 
     [5] => 003 
    ) 
[new02] => Array 
    (
     [0] => 004 
     [1] => 004 
     [2] => 005 
     [3] => 006 
     [4] => 007 
     [5] => 008 
    ) 
    ... 
) 

我需要以某種方式結合array_merge_recursive()功能和foreach環......

回答

0

下面是一個使用的解決方案array_walk:

array_walk($arr, function($item, $key, &$d) { 
    if ($d[0]++ > 1 && $d[0] % 2) { 
     $end = &$d[1][count($d[1]) - 1]; 
     $end = array_merge($end, $item); 
    } 
    else { 
     $d[1][] = $item; 
    } 
}, array(0, &$newarr)); 

而這裏是一個工作demo

+0

真棒,它完美的作品!非常感謝你! –