Question: I have this array:

```
$array = array(a, b, c, d, e, f, g);

```

I want to split it in two arrays depending if the index is even or odd, like this:

```
$odd = array(a, c, e, g);

$even = array(b, d, f);

```

Thanks in advance!

Answer: 
One solution, using anonymous functions and [`array_walk`](http://php.net/manual/en/function.array-walk.php):

```
$odd = array();
$even = array();
$both = array(&$even, &$odd);
array_walk($array, function($v, $k) use ($both) { $both[$k % 2][] = $v; });

```

This separates the items in just one pass over the array, but it's a bit on the "cleverish" side. It's not really any better than the classic, more verbose

```
$odd = array();
$even = array();
foreach ($array as $k => $v) {
    if ($k % 2 == 0) {
        $even[] = $v;
    }
    else {
        $odd[] = $v;
    }
}

```
One

```
$odd = $even = array();
for ($i = 0, $l = count($array ); $i < $l;) { // Notice how we increment $i each time we use it below, by two in total
    $even[] = $array[$i++];
    if($i < $l)
    {
       $odd[] = $array[$i++];
    }
}

```

Two

```
$odd = $even = array();
foreach (array_chunk($array , 2) as $chunk) {
    $even[] = $chunk[0];
    if(!empty( $chunk[1]))
    {
       $odd[] = $chunk[1];
    }
}

```