我有一个动态数量的项目,我需要将其分成列.比方说我得到了这个:
array("one", "two", "three", "four", "five", "six", "seven", "eight")
Run Code Online (Sandbox Code Playgroud)
我需要生成这个:
<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>
Run Code Online (Sandbox Code Playgroud)
以下是一些规则:
<ul>到目前为止我所拥有的:
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 }
}
Run Code Online (Sandbox Code Playgroud)
我输入一个数字,比如divide(4,$ arrRef),数字决定列的数量,但是我需要重构,所以它决定了列数
我在视图模板中使用了这段代码.
<?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>";
}; ?>
Run Code Online (Sandbox Code Playgroud)