php split array into smaller even arrays

EDIT: There’s array_chunk, which does just that. Well, I didn’t feel like debugging, so I wrote a version with array_reduce: $pergroup = 2; $redfunc = function ($partial, $elem) use ($pergroup) { $groupCount = count($partial); if ($groupCount == 0 || count(end($partial)) == $pergroup) $partial[] = array($elem); else $partial[$groupCount-1][] = $elem; return $partial; }; $arr = array(1,2,3,4,5); … Read more