3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
New Raw <?php $_rows = array(); //num of columns each row should have $rows = array(2, 3, 3, 3, 2, 3, 3, 3, 3, 3); $catIncrement = 0; foreach($rows as $rowNumber => $numColumns) { for($i = 0; $i < $numColumns; $i++) { $catIncrement++; $_rows['row' . ($rowNumber + 1)][] = 'Category ' . $catIncrement; } } $_containers = array(); //num of rows in each container $containers = array(1, 3, 1, 5); $rowIncrement = 0; foreach($containers as $contNumber => $numRows) { for($i = 0; $i < $numRows; $i++) { $rowIncrement++; $_containers['cont' . ($contNumber + 1)][] = $_rows['row' . $rowIncrement]; } } foreach($_containers as $_contKey => $rows):?> <?php $firstRow = true; ?> <?php foreach($rows as $rowKey => $categories): ?> <?php $firstCategory = true; ?> <?php foreach($categories as $catKey => $category): ?> <?php //first row -- print open <ul> ?> <?php if($firstRow): ?> <?php $firstRow = false; ?> <?php //if only 2 categories in this row ?> <?php if(count($categories) == 2): ?> <ul class="foundry-listing two-up"> <?php else: ?> <ul class="foundry-listing"> <?php endif; ?> <?php endif; ?> <?php //first category in each row ?> <?php if($firstCategory): ?> <li class="first"><?php echo $category; ?></li> <?php $firstCategory = false; ?> <?php else: ?> <li><?php echo $category; ?></li> <?php endif; ?> <?php endforeach; ?> <?php endforeach; ?> </ul> <?php endforeach; ?>
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
New Raw <ul class="foundry-listing two-up"> <li class="first">Category 1</li> <li>Category 2</li> </ul> <ul class="foundry-listing"> <li class="first">Category 3</li> <li>Category 4</li> <li>Category 5</li> <li class="first">Category 6</li> <li>Category 7</li> <li>Category 8</li> <li class="first">Category 9</li> <li>Category 10</li> <li>Category 11</li> </ul> <ul class="foundry-listing two-up"> <li class="first">Category 12</li> <li>Category 13</li> </ul> <ul class="foundry-listing"> <li class="first">Category 14</li> <li>Category 15</li> <li>Category 16</li> <li class="first">Category 17</li> <li>Category 18</li> <li>Category 19</li> <li class="first">Category 20</li> <li>Category 21</li> <li>Category 22</li> <li class="first">Category 23</li> <li>Category 24</li> <li>Category 25</li> <li class="first">Category 26</li> <li>Category 27</li> <li>Category 28</li> </ul>