Skip to content

Instantly share code, notes, and snippets.

@trevorsheridan
Created December 28, 2011 18:18
Show Gist options
  • Save trevorsheridan/1528988 to your computer and use it in GitHub Desktop.
Save trevorsheridan/1528988 to your computer and use it in GitHub Desktop.
Iterator that takes a single dimensional array as input and produces a multidimensional array of groups of three
<?php
$lc = sizeof($list);
for ($i = 0; $i < ( ($lc % 3 == 0) ? $lc : ($lc - ($lc % 3) + 3) ); $i = $i + 3) { // This loop defines the group you're in, $i will always be divisible by 3.
for ($si = 0; $si < ( ($i + 3 < $lc) ? 3 : ($lc - $i) ); $si++) // Determine which item is next in this group.
$a[] = list[$i + $si];
$groups[] = $a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment