我希望能够定义列数,但是,这并没有定义列。当您看到输出时,您会注意到它更改了行数而不是列数。
我已经尽可能详细地记录了,感谢您的所有帮助!第一次发帖,如有错误请多多包涵:)
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