【问题标题】:How to show the array content in php如何在php中显示数组内容
【发布时间】:2021-02-02 18:30:11
【问题描述】:

我有一个看起来像这样的数组:

Array
(
    [0] => Array
        (
            [total words] => 1476
        )

    [1] => Array
        (
            [keyword] => difference
            [count] => 82
            [percent] => 5.56
        )

    [2] => Array
        (
            [keyword] => 2010
            [count] => 37
            [percent] => 2.51
        )

    [3] => Array
        (
            [keyword] => very
            [count] => 22
            [percent] => 1.49
        )

)

我想在一个三列三行的表格中显示数组内容。每行包含关键字、计数和百分比作为列和总计。单词将显示在表格标题中。

请帮帮我!我正在尝试运行一个 for 循环,但不知道如何显示数组内容,因为它看起来像一个多维数组。请帮帮我。

【问题讨论】:

    标签: php arrays html-table show


    【解决方案1】:

    这应该是你所追求的。

    print '<table>';
    $headers = array_keys(reset($array));
    
    print '<tr>';
    foreach($headers as $header){
        print '<th>'.$header.'</th>';
    }
    print '<tr>';
    
    foreach($array as $row){
        print '<tr>';
        foreach($row as $col){
            print '<td>'.$col.'</td>';
        }
        print '</tr>';
    }
    print '</table>';
    

    【讨论】:

    • 为什么它需要是一个for 循环? foreach 有什么问题?
    【解决方案2】:

    在这种情况下,Implode 是你的朋友:

    $arrayLength = count($myArray);
    
    echo '<table>';
    
    for($i=0;$i<$arrayLength;$i++){
    
       echo '<tr><td>'.
           .implode('</td><td>',$myArray[$i])
           .'</td></tr>';
    
    }
    echo '</table>';
    

    http://us2.php.net/manual/en/function.implode.php

    【讨论】:

      【解决方案3】:

      您可以使用foreach($array =&gt; $value) 循环遍历您的数组。
      这段代码应该可以解决问题:

      <table>
      <tr>
          <th>Keyword</th>
          <th>Count</th>
          <th>%</th>
      </tr>
      <?php foreach ( $data as $row ): ?>
      <tr>
          <td><?php echo $row['keyword']; ?></td>
          <td><?php echo $row['count']; ?></td>
          <td><?php echo $row['percent']; ?></td>
      </tr>
      <?php endforeach; ?>
      </table>
      

      【讨论】:

        【解决方案4】:

        假设数组存储在变量 $a 中。

        foreach($item in $a) {
          echo $item['keyword'] . " " . $item['count'] . " " .  $item['percent'] . "<br>\n";
        }
        

        【讨论】:

          【解决方案5】:

          array_values — 返回一个数组的所有值

          print_r(array_values($array));
          

          【讨论】:

            【解决方案6】:

            除了上面 Godea Andrei 的回答,如果您使用 print_r,请使用

                print_r($array)
            

            在 print_r 调用中调用 array_values 将去除数组元素的所有关联命名。如果只使用数字数组索引,它仍然会显示索引值。例如

                $a = array("red", "green", "blue");
            

            print_r($a) 将显示

                Array ( [0] => red [1] => green [2] => blue )
            

                $b = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
            

            print_r($b) 将显示

                Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
            

            如果使用 array_values,第二个示例的输出将是

                Array ( [0] => 35 [1] => 37 [2] => 43 )
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-01-21
              • 2019-01-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多