【问题标题】:PHP Parsing Hotel data (name, location etc..) and print them with foreach loop in TablePHP 解析酒店数据(名称、位置等)并在表中使用 foreach 循环打印它们
【发布时间】:2020-09-21 13:21:13
【问题描述】:
$curl = curl_init();
$response = curlPost($curl, $url, $headers, http_build_query($body));

$data = json_decode($response, true);


if ($data["success"] === true) {
  $hotels = $data["hotels"];

foreach ($hotels as $hotel) {
    echo $hotel['name']."<br>";

    foreach ($hotel as $key => $value) {
        if ($key === "id"){
            continue;
        }
        if ($key === "name"){
            continue;
        }
        echo "{$key}: {$value}" ."<br>";
    }
    echo PHP_EOL . PHP_EOL."<hr>";
}
 } else {
 echo $data["reason"];
}
?>
<table>
    <thead>
    <tr>
        <th colspan="2"> NAME OF THE HOTEL </th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>key</td>
        <td>value</td>
    </tr>
    </tbody>
</table>
<hr/>

这是我的代码。如您所见,我正在用 curl 解析一些数据,更具体地说是一些酒店的名称、ID、位置等,并将它们存储在一个名为 data 的变量中。我知道如何打印数据,但我不知道如何将数据放在下表中。例如,我想说&lt;th colspan="2"&gt; HOTEL PARIS HILTON &lt;/th&gt;&lt;td&gt;location&lt;/td&gt; &lt;td&gt;USA&lt;/td&gt;etc 下方。 帮助任何人?谢谢

【问题讨论】:

    标签: php arrays foreach


    【解决方案1】:

    在循环中,您实际上可以回显您的表格内容:

    <table>
    <?php
    foreach ($hotels as $hotel) {
        $columnsCount = count($hotel) - 2;
    
        echo '
        <tr>
            <td colspan="' . $columnsCount . '">
                ' . $hotel['name'] . '
            </td>
        </tr>
        <tr>';
    
    
        foreach ($hotel as $key => $value) {
            if ($key === 'id' || $key === 'name'){
                continue;
            }
    
            echo '<td>' . $value . '</td>';
        }
    
        echo '</tr>';
    }
    ?>
    </table>
    

    您的评论指的是常规的表格渲染,这与我最初给您的没有太大区别。

    <table>
      <thead>
        <tr>
          <th>
            <?php
                $columns = array_keys($hotels[0]);
                echo implode('</th><th>', array_diff($columns, ['id']));
            ?>
          </th>
        </tr>
      </thead>
      <tbody>
        <?php 
            foreach ($hotels as $hotel) {
                $hotel = array_diff_key($hotel, ['id' => false]);
                echo '<tr><td>' . implode('</td><td>', $hotel) . '</td></tr>';
            }
        ?>
      </tbody>
    </table>
    

    【讨论】:

    • 我知道你在那里做了什么,但你能从上到下做吗?像这样的表https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_intro
    【解决方案2】:
    foreach ($hotels as $hotel) {
        echo "<table>";
        echo "<thead>";
        echo "<tr>";
        echo "<th colspan='2'> {$hotel['name']} </th>";
        echo "</tr>";
        echo "<thead>";
    
        foreach ($hotel as $key => $value) {
            if ($key === "id"){
                continue;
            }
            if ($key === "name"){
                continue;
            }
                echo "<tbody>";
                echo "<tr>";
                echo "<td>{$key}</td> "."<td>{$value}</td>"."<br>";
                echo "</tr>";
                echo " </tbody>";
        }
        echo    "</table>";
        echo    "<hr/>";
    }
      } else {
       echo $data["reason"];
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-08
      • 2019-10-31
      相关资源
      最近更新 更多