Created
February 24, 2011 22:47
-
-
Save thinkt4nk/843064 to your computer and use it in GitHub Desktop.
dynamic two-column list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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