Php 将动态数量的项划分为列
我有一个动态数量的项目,我需要在其中划分为列。假设我得到了这个:Php 将动态数量的项划分为列,php,pagination,Php,Pagination,我有一个动态数量的项目,我需要在其中划分为列。假设我得到了这个: array("one", "two", "three", "four", "five", "six", "seven", "eight") 我需要生成以下内容: <ul> <li>one</li> <li>two</li> <li>three</li> <li>four</li> </ul> &
array("one", "two", "three", "four", "five", "six", "seven", "eight")
我需要生成以下内容:
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
<li>four</li>
</ul>
<ul>
<li>five</li>
<li>six</li>
<li>seven</li>
<li>eight</li>
</ul>
- 一个
- 两个
- 三
- 四
- 五
- 六
- 七
- 八
以下是一些规则:
- 如果没有物品,我不想吐出任何东西
- 如果有16项或16项以下,我希望每个
- 如果有超过16件物品,我希望它能均匀分布
- 我必须按字母顺序重新订购物品。如果有17项,第17项将需要转到第一列,但所有内容都需要重新排序
function divide( $by, $array ) {
14 $total = count( $array );
15 $return = array();
16 $index=0;
17 $remainder = $total % $by !== 0;
18 $perRow = $remainder ?
19 $total / $by + 1:
20 $total / $by
21 ;
22
23 for ( $j = 0; $j<$by; $j++ ) {
24 //$return[] = array();
25
26 if ( $index == 0 ) {
27 $slice = array_slice( $array, 0, $perRow );
28 $index = $perRow;
29 $return[$j] = $slice;
30 } else {
31 $slice = array_slice( $array, $index, $perRow );
32 $index = $index+$perRow;
33 $return[$j] = $slice;
34 }
35 }
}
函数除($by,$array){
14$total=计数($array);
15$return=array();
16美元指数=0;
17$余数=$total%$by!==0;
18美元佩罗=剩余美元?
19美元总额/$by+1:
总计20美元/$by
21 ;
22
23对于($j=0;$j未测试:
函数除法($by,$list,$order='c'){
如果(空($列表)){
返回数组();
}
如果(计数($list)0-1:0);
对于($cIndex=0;$cIndex<$by;$cIndex++){
$columnLists[$cColumn]=数组_切片($list,$cIndex*$maxColumnHeight,$maxColumnHeight);
}
打破
}
返回$columnLists;
}
您的要求非常奇怪/模糊,但我认为这符合您的要求
function columnize( array $set, $maxCols = 4 )
{
if ( count( $set ) <= 16 )
{
return array_chunk( $set, 4 );
}
return array_chunk( $set, ceil( count( $set ) / $maxCols ) );
}
函数列化(数组$set,$maxCols=4)
{
如果(count($set)如果我理解正确,您希望“智能地”将项目划分为四列,以便在并排放置时可以逻辑地读取列表。如果是这样,这应该可以做到:
function columnize($items, $columns = 4, $min_per_column = 4) {
if(empty($items)) return array();
$result = array();
$count = count($items);
$min_count = $min_per_column * $columns;
if($count <= $min_count) {
$columns = ceil($count / $min_per_column);
} else {
$per_column = floor($count / $columns);
$overflow = count($items) % $columns;
}
for($column = 0; $column < $columns; $column++) {
if($count <= $min_count) {
$item_count = $min_per_column;
} else {
$item_count = $per_column;
if($overflow > 0) {
$item_count++;
$overflow--;
}
}
$result[$column] = array_slice($items, 0, $item_count);
$items = array_slice($items, $item_count);
}
return $result;
}
函数列化($items,$columns=4,$min\u per\u column=4){
if(空($items))返回数组();
$result=array();
$count=计数($items);
$min_count=$min_per_column*$columns;
如果($count我在视图模板中使用了此代码
<?php
$col = 3;
$projects = array_chunk($projects, ceil(count($projects) / $col));
foreach ($projects as $i => $project_chunk)
{
echo "<ul class='pcol{$i+1}'>";
foreach ($project_chunk as $project)
{
echo "<li>{$project->name}</li>";
};
echo "</ul>";
}; ?>
有趣的问题。我建议您不要立即接受答案,因为用一个真正好的答案回答这个问题可能需要一些时间。“如果有超过16项,我希望它均匀分布”?定义“均匀分布”。您的条件不是自一致的:重新排序。您希望连续项按行顺序(示例中暗示)还是按列顺序(暗示为“如果有17项,则第17项需要转到第一列”)正如Karl所建议的,我认为应该有更多的规则。例如,如果有100个项,那么应该有多少列?4、5、10等等。这是一个非常常见的问题,并且是有效的分页。虽然我认为这应该在函数外部进行,但出于OPs的目的,在函数内部进行数组排序可能是一个好主意,正如我们所说的陆上通信线。
<?php
$col = 3;
$projects = array_chunk($projects, ceil(count($projects) / $col));
foreach ($projects as $i => $project_chunk)
{
echo "<ul class='pcol{$i+1}'>";
foreach ($project_chunk as $project)
{
echo "<li>{$project->name}</li>";
};
echo "</ul>";
}; ?>