PHP:如何配对数组值

时间:2013-04-24 06:19:40

标签: php arrays

数组就像..

array ( 
      [0] => a
      [1] => b
      [2] => c
      [3] => d
      [4] => e
      [5] => f
      [6] => g
      [7] => h
     )

我希望他们配对......

Array 
( [0] => Array ( 
    [0] => a
    [1] => b)

  [1] => Array ( 
    [0] => c
    [1] => d) 

  [2] => Array ( 
    [0] => e
    [1] => f)

  [3] => Array ( 
    [0] => g
    [1] => h)
)

我该怎么做?有8个值,我希望从这8个值中有4对。我是php的新手,所以我真的不太了解php,我搜索了谷歌,但无法找到解决方案。

1 个答案:

答案 0 :(得分:7)

您可以使用array_chunk()

print_r(array_chunk($arr, 2));

Demo