php数组块与数组的下一个元素

时间:2017-06-02 07:09:31

标签: php arrays

$ arrayinput = array(“a”,“b”,“c”,“d”,“e”);

我如何能够实现以下输出......

输出:     排列     (         [0] =>排列             (                 [0] =>一个                 [1] => b             )

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

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

[3] => Array
    (
        [0] => d
        [1] => e
    )

[4] => Array
    (
        [0] => e
    )

2 个答案:

答案 0 :(得分:0)

您可以使用此live demo here

<?php
$arrayinput = array("a","b","c","d","e");

$array = [];
foreach($arrayinput as $v)
{
  $arr = [];
  $arr[] = $v;
  if($next = next($arrayinput))
    $arr[] = $next;
  $array[] = $arr;
}
print_r($array);

答案 1 :(得分:0)

这里的实例:http://sandbox.onlinephpfunctions.com/code/4de9dda457de92abdee6b4aec83b3ccff680334e

$arrayinput = array("a","b","c","d","e");
$result = [];

for ($x = 0; $x < count($arrayinput); $x+=2 ) {
    $tmp = [];
    $tmp[] = $arrayinput[$x];
    if ($x+1 < count($arrayinput)) $tmp[] = $arrayinput[$x+1];
    $result[] = $tmp;
}

var_dump($result);