我希望能够定义列数,但是,这不会定义列。当您看到输出更改行数而不是列数时,您会注意到。
我已尽可能详细地记录,感谢您的帮助!如果我犯了错误,第一次张贴就这样对我说:)
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
答案 0 :(得分:1)
你的事情太复杂了!使用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 -->';
}
答案 1 :(得分:0)
您可以更简单的方式获得预期的输出:
$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