Skip to content

Instantly share code, notes, and snippets.

@thinkt4nk
Created February 24, 2011 22:47
Show Gist options
  • Save thinkt4nk/843064 to your computer and use it in GitHub Desktop.
Save thinkt4nk/843064 to your computer and use it in GitHub Desktop.
dynamic two-column list
<?php
$list = array('this','that','theother','andthis');
$evenList = (count($list)%2==0) ? true : false;
$listOne = array();
$listTwo = array();
if( $evenList ) {
$half = count($list)/2;
$listOne = array_slice($list,0,$half);
$listTwo = array_slice($list,($half-1));
} else {
$half = round((count($list)/2),PHP_ROUND_HALF_DOWN);
// put extra element in first list
$listOne = array_slice($list,0,($half+1));
$listTwo = array_slice($list,($half-1));
}
$lists = array($listOne,$listTwo);
foreach($lists as $l) {
foreach($l as $elementInList) {
// do stuff
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment