我希望能够定义列数,但是,这不定义列。 当您看到输出时,您会注意到它更改了行数而不是列数。
我已经尽可能彻底地记录了,感谢您的所有帮助!第一次发帖,如果我犯了错误,请耐心等待:)
public function SplitIntoColumns(array $arrayTranspose=array(), $no_of_cols=3,$min=0) {
$result = '<div class="column">'."'n";
// The 2D array
$colArr = array();
// The tmpArray
$tmpArray = array();
if (count($arrayTranspose) <= $min) {
$result .= implode("'n", $arrayTranspose);
}
else {
// Set size of the array
$cnt = intval(floor(count($arrayTranspose)));
$row = 0;
$col = 0;
// Create Multidimensional array from Single dimensional array
foreach($arrayTranspose as $key => $val){
$colArr[$col][$row] = $arrayTranspose[$key];
// Store the data from the array
$result .= $colArr[$col][$row]." ";
$col++;
// If columns are equal to the set value then start
// From the beginning on a new row
if($col == $no_of_cols) {
$col = 0;
$row++;
// create columns
$result .= '</div><div class="column">'."'n";
}
}
}
// This says numberOfRows, but it is actually the number of columns
$numberOfRows = intval((floor($cnt) / $no_of_cols));
// A loop to transpose into columns
for($i = 0; $i <= $no_of_cols; $i++) {
for($j = 0; $j <= $numberOfRows; $j++){
$tmpArray[$j][$i] = $colArr[$i][$j];
$result2 .= $tmpArray[$j][$i].' ';
}
$result2 .= '</div><div class="column">'."'n";
}
// return stuff
$result2 .= "</div> <!-- /.column -->'n";
return $result2;
}
}
$Util = new SplitColumns();
$content = array('Lorem ipsum','elit sed do','labore et dolore', 'dolor sit amet', 'eiusmod tempor', 'magna aliqua', 'consectetur adipisicing', 'incididunt ut');
// This is where you can define the number of columns you want
// However, this does not define columns. You will notice when
// You see the output that it changes the number of rows
echo $Util->SplitIntoColumns($content, 4);
此输出:
Lorem ipsum eiusmod tempor
elit sed do magna aliqua
labore et dolore consectetur adipisicing
dolor sit amet incididunt ut
所需的输出应为:
Lorem ipsum labore et dolore eiusmod tempor consectetur adipisicing
elit sed do dolor sit amet magna aliqua incididunt ut
你把事情复杂化了!将array_chunk()
与一些简单的数学结合使用,以正确设置数据的格式:
function splitIntoColumns($array, $columns=3)
{
$numberOfRows = ceil(count($array) / $columns);
return array_chunk($array, $numberOfRows);
}
这给你留下了一个列数组 - $returnValue[0] 是第 0 列中的元素数组,依此类推,直到列限制。示例输出(4 列):
array(4) {
[0]=>
array(2) {
[0]=>
string(11) "Lorem ipsum"
[1]=>
string(11) "elit sed do"
}
[1]=>
array(2) {
[0]=>
string(16) "labore et dolore"
[1]=>
string(14) "dolor sit amet"
}
[2]=>
array(2) {
[0]=>
string(14) "eiusmod tempor"
[1]=>
string(12) "magna aliqua"
}
[3]=>
array(2) {
[0]=>
string(23) "consectetur adipisicing"
[1]=>
string(13) "incididunt ut"
}
}
从那里开始,HTML的添加应该相当简单,可以通过多种方式完成,例如:
$result = splitIntoColumns($inputArray, 3);
foreach($result as $column => $columnMembers)
{
echo '<div class="column">';
foreach($columnMembers as $key => $val)
{
echo $val;
//or whatever else you want to do in here
}
echo '</div> <!-- /column -->';
}
编辑:带有 html 输出的示例
您可以通过更简单的方式获得预期的输出:
$content = array('Lorem ipsum','elit sed do','labore et dolore', 'dolor sit amet', 'eiusmod tempor', 'magna aliqua', 'consectetur adipisicing', 'incididunt ut');
for($i = 0; $i<count($content); $i++){
if($i % 2 == 0){
$col1 .= $content[$i] . ' ';
}
else{
$col2 .= $content[$i] . ' ';
}
}
echo $col1 . "'n" . $col2;
输出:
Lorem ipsum labore et dolore eiusmod tempor consectetur adipisicing
elit sed do dolor sit amet magna aliqua incididunt ut