【问题标题】:Display Arrays Data In Custom Order In PHP在 PHP 中以自定义顺序显示数组数据
【发布时间】:2017-02-07 23:25:58
【问题描述】:

我想以自定义顺序显示数组数据。

我的示例数组是:

['a' => 'a', 
'b' => 'b', 
'c' => 'c', 
'd' => 'd', 
'e' => 'e', 
'f' => 'f',]

我已尝试使用以下代码:

<table>
<tr>
<?php
    $count = 0;
    foreach ($support_others as $other) {
        echo "<td>" . $other ."</td>";
        $count++;
        if ((($count % 3) == 0) && ($count > 0)) {
            echo ("</tr><tr>");
        }
    }
?>
</tr>
</table>

其显示顺序为

a b c

d e f

但我想显示为:

一丁点

b e

c f

任何帮助将不胜感激。

【问题讨论】:

    标签: php arrays sorting


    【解决方案1】:

    我总是喜欢用带有两个计数器的循环来处理这样的事情。看看这个解决方案:

    <?php
    
    $support_others = [
      'a' => 'a', 
      'b' => 'b', 
      'c' => 'c', 
      'd' => 'd', 
      'e' => 'e', 
      'f' => 'f'
    ];
    
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
      <title></title>
    </head>
    <body>
      <table>
    
        <?php
    
          for ($i = 'a', $j = 'd'; $i < 'd', $j < 'g'; $i++, $j++) {
            echo '<tr>';
            echo '<td>' . $support_others[$i] . '</td>';
            echo '<td>' . $support_others[$j] . '</td>';
            echo '</tr>';
          }
    
        ?>
    
      </table>
    
    </body>
    </html>
    

    有关更通用的版本,请参见此处:

    <?php
    
        $columnOneStart = 'a';
        $columnOneEnd = 'c';
    
        $columnTwoStart = 'd';
        $columnTwoEnd = 'f';
    
          for ($i = $columnOneStart, $j = $columnTwoStart; $i <= $columnOneEnd, $j <= $columnTwoEnd; $i++, $j++) {
            echo '<tr>';
            echo '<td>' . $support_others[$i] . '</td>';
            echo '<td>' . $support_others[$j] . '</td>';
            echo '</tr>';
          }
    
    ?>
    

    【讨论】:

    • 如何显示更大的数组?让我们说60个项目。每行 3 列 20 项?
    • @HappyNinja 看看你能不能自己解决这个问题!我将编辑我的答案并使其更“通用”,以便您了解边界条件是如何工作的。如果您经过一些尝试和错误后无法弄清楚,我会帮助您更多。
    • 好的,您有 60 个项目,并且您希望第 1 列中的项目 1-20、第 2 列中的 21-40 和第 3 列中的 41-60 正确吗?你现在能看到如何调整上面的例子来满足你的需要了吗?
    • 我用有限的数据得到了你的例子。它将如何推广到更大的数组 - 任何想法?
    • 是的,我知道怎么做,我试图帮助你自己做,这样你就可以了解它是如何工作的。您能看到在我的通用版本(我的答案的第二部分)中,循环计数器设置为 columnOneStart/columnTwoStart,循环继续条件设置为 columnOneEnd/columnTwoEnd。使用它,您能看到如何将其扩展为具有 columnThreeStart 和 columnThreeEnd 吗?
    【解决方案2】:

    在这种表格显示中,关键是每行的元素数,它可以让您计算行数和行的第一个元素。 找到后,您只需添加列号即可使“指针”移动到“行”元素。

    <?php
    
    $example = [
        'a' => 'a', 
        'b' => 'b', 
        'c' => 'c', 
        'd' => 'd', 
        'e' => 'e', 
        'f' => 'f'
    ];
    $elements = range('a', 'z');
    
    function customTable($elements, $elementsPerRow = 2){
        $elements = array_values($elements);
        $count = count($elements);
    
        // Prevent empty cols and unallowed (<0 && >n)
        $elementsPerRow = !($elementsPerRow < $count/2 && $elementsPerRow > 0)
            ? (($elementsPerRow > $count) ? $count : 1)
            : $elementsPerRow;
        $rows = ceil($count / $elementsPerRow);
    
        $table = '<table>';
        for($i = 0; $i < $rows; ++$i){
            $table .= '<tr>';
            for($j = 0; $j < $elementsPerRow; ++$j){
                $table .= '<td>' . (isset($elements[$j*$rows+$i]) ? $elements[$j*$rows+$i] : '') . '</td>';
                // For debug purpose only
                $table .= '<td>' . ($j*$rows+$i) . '</td>';
    
            }
            $table .= '</tr>';
        }
        return $table . '</table>';
    }
    
    ?>
    
    <!DOCTYPE HTML>
    <html>
    <head>
        <style type="text/css">
            table { border-collapse : collapse; margin: 1em;}
            td { border: 1px solid black; padding: 0.5em; }
        </style>
    </head>
    <body>
    <?php
    echo customTable($elements, 2);
    echo customTable($elements, 3);
    echo customTable($elements, 4);
    echo customTable($elements, 30);
    echo customTable($example, 2);
    ?>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 2012-02-14
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多