Splitting an array into sections might be useful for setting up a calendar or pagination on a site. Either way there are numerous ways to do this but the following seems to be the quickest and most reliable method.
function sectionArray($array, $step) { $k = 0; if ( !($i % $step) ) { $k++; } $sectioned[$k][] = $array[$i]; } return $sectioned; }
Run the function by passing it an array, in this case I am going to split the alphabet into 5 arrays of 5 letters.
This produces the following output.
Array ( [1] => Array ( [0] => a [1] => b [2] => c [3] => d [4] => e ) [2] => Array ( [0] => f [1] => g [2] => h [3] => i [4] => j ) [3] => Array ( [0] => l [1] => m [2] => n [3] => o [4] => p ) [4] => Array ( [0] => q [1] => r [2] => s [3] => t [4] => u ) [5] => Array ( [0] => v [1] => w [2] => x [3] => y [4] => z ) )
0 comments:
Post a Comment