因为我不是 100% 清楚你的问题中的“美丽”是什么意思,所以我对什么是可以接受的做了一些假设。
以下代码最多可用于 100 个菜单配置。
- 它首先尝试将项目分配到 3、4、5 或 6 行(60% 的案例)
- 然后它试图在最后一行只留下 1 个(30% 的案例)
- 然后它试图在最后一行只留下 2 个(10% 的案例)
编码:
<?php
$column_range = range(3,6);
#$menu = array(16,22,10,19);
$menu = range(1,100); # showing a range of menu configurations
print_r($menu); print '<hr />';
foreach ($menu as $item){
$beautiful = False;
foreach($column_range as $column_width){
if($item%$column_width==0 && $beautiful==False){
print "$item will be beautiful if you use $column_width per row";
print '<br />';
$beautiful = True;
}
}
if($beautiful==False){
foreach($column_range as $column_width){
if($item%$column_width==1 && $beautiful==False){
print "$item is hard to make beautiful, most rows will have $column_width, however the last row will have 1 one it";
print '<br />';
$beautiful = True;
}
}
}
if($beautiful==False){
foreach($column_range as $column_width){
if($item%$column_width==2 && $beautiful==False){
print "$item is hard to make beautiful, most rows will have $column_width, however the last row will have 2 one it";
print '<br />';
$beautiful = True;
}
}
}
if($beautiful==False){
print "$item what shall i do with you";
print '<br />';
}
}
?>