eval.in

Paste #4496

PHP — PHP 5.4.6, pasted 1 year ago

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<?php

$array = [0, 1, 2, 3, 0, 4, 5];
$keys  = array_keys($array, 0);
$parts = [];
$last  = 0;

foreach ($keys as $key)
{
    $last !== $key 
        && $parts[] = array_slice($array, $last, $key - $last, true);    
    $last = $key;
}

$parts[] = isset($key) ? array_slice($array, $key, null, true) : $array;

var_dump($parts);

Program Output

array(2) {
  [0]=>
  array(4) {
    [0]=>
    int(0)
    [1]=>
    int(1)
    [2]=>
    int(2)
    [3]=>
    int(3)
  }
  [1]=>
  array(3) {
    [4]=>
    int(0)
    [5]=>
    int(4)
    [6]=>
    int(5)
  }
}

OK (0.004 sec real, 0.005 sec wall, 13 MB, 74 syscalls)

Fork