将PHP数组拆分为5个列

时间:2016-05-04 13:33:57

标签: php

如何实现如下导航菜单:

I have to use all colums if value are greater than 5. 我从foreach循环获取数据,我想制作如下的结构。我必须使用 foreach循环

<?php
    $customArray = array('abc','def','ghi','jkl','mno','pqr');
    $col = 5;
    $menus = count($customArray);
?>

我想要下面的结构,例如,如果6个数组值如下所示,

<?php foreach($customArray as $key=>$value) {?>
     <ul class="test">
        <li class="0">
            <ul>
                <li>abc</li>
                <li>def</li>
            </ul>
        </li>
        <li class="1">ghi</li>
        <li class="2">jkl</li>
        <li class="3">mno</li>
        <li class="4">pqr</li>
    </ul> 
<?php } ?>

如果数组值为7,那么结构如下所示,

 <?php foreach($customArray as $key=>$value) {?>
     <ul class="test">
        <li class="0">
            <ul>
                <li>abc</li>
                <li>def</li>
            </ul>
        </li>
        <li class="1">
            <ul>
                <li>ghi</li>
                <li>jkl</li>
            </ul>
        </li>
        <li class="2">mno</li>
        <li class="3">pqr</li>
        <li class="4">stu</li>
    </ul>
 <?php } ?>

如果数组值为8,则

<ul class="test">
    <li class="0">
        <ul>
            <li>abc</li>
            <li>def</li>
        </ul>
    </li>
    <li class="1">
        <ul>
            <li>abc</li>
            <li>def</li>
        </ul>
    </li>
    <li class="2">
        <ul>
            <li>abc</li>
            <li>def</li>
        </ul>
    </li>
    <li class="3">abc</li>
    <li class="4">abc</li>
</ul>

如果数组值为9,则

<ul class="test">
    <li class="0">
        <ul>
            <li>abc</li>
            <li>def</li>
        </ul>
    </li>
    <li class="1">
        <ul>
            <li>abc</li>
            <li>def</li>
        </ul>
    </li>
    <li class="2">
        <ul>
            <li>abc</li>
            <li>def</li>
        </ul>
    </li>
    <li class="3">
        <ul>
            <li>abc</li>
            <li>def</li>
        </ul>
    </li>
    <li class="4">abc</li>
</ul>

如何实现这种结构。

2 个答案:

答案 0 :(得分:3)

你可以这样做: -

while (! empty($customArray)) {
  $chunks[] = array_splice($customArray, 0, ceil(count($customArray) / $cols--));
}

因此,请继续拆分数组,并减少可用$cols的数量,直到$customArray为空。

例如,说$customArray有6个条目:

1st iteration: ceil(6 / 5) = 2
2nd iteration: ceil(4 / 4) = 1
3rd iteration: ceil(3 / 3) = 1
4th iteration: ceil(2 / 2) = 1
5th iteration: ceil(1 / 1) = 1

如果$customArray有9个条目:

1st iteration: ceil(9 / 5) = 2 
2nd iteration: ceil(7 / 4) = 2
3rd iteration: ceil(5 / 3) = 2
4th iteration: ceil(3 / 2) = 2
5th iteration: ceil(1 / 1) = 1

使用$customArray = range('A', 'Z')(26个条目)输出

Array
(
    [0] => Array
        (
            [0] => A
            [1] => B
            [2] => C
            [3] => D
            [4] => E
            [5] => F
        )

    [1] => Array
        (
            [0] => G
            [1] => H
            [2] => I
            [3] => J
            [4] => K
        )

    [2] => Array
        (
            [0] => L
            [1] => M
            [2] => N
            [3] => O
            [4] => P
        )

    [3] => Array
        (
            [0] => Q
            [1] => R
            [2] => S
            [3] => T
            [4] => U
        )

    [4] => Array
        (
            [0] => V
            [1] => W
            [2] => X
            [3] => Y
            [4] => Z
        )

)

然后你可以循环输出它:

foreach ($chunks as $key => $chunk) {
  echo '<li class="' . $key . '">';
  if (count($chunk) == 1) {
    echo $chunk[0];
  } else {
    echo '<ul><li>'. implode('</li><li>', $chunk) . '</li></ul>';
  }
  echo '</li>';
}

完整示例:

$customArray = range(1, 100);
$cols = 5;
$chunks = array();

while (! empty($customArray)) {
  $chunks[] = array_splice($customArray, 0, ceil(count($customArray) / $cols--));
}

foreach ($chunks as $key => $chunk) {
  echo '<ul class="' . $key . '">';
  echo (count($chunk) == 1) ? $chunk[0] : '<ul><li>' . implode('</li><li>', $chunk) . '</li></ul>';
  echo '</ul>';
}

答案 1 :(得分:0)

使用JS可以解决这个问题,

var table = document.getElementById("myTable").rows;
var y;
for(i = 0; i < # of rows; i++)
{    for(j = 0; j < # of columns; j++)
     {
         y = table[i].cells;
         //do something with cells in a row
         y[j].innerHTML = "";
     }
}