PHP解析酒店数据(名称、位置等),并使用表中的foreach循环打印它们 $curl=curl_init(); $response=curlPost($curl,$url,$headers,http_build_query($body)); $data=json_decode($response,true); 如果($data[“success”]==true){ $hotels=$data[“hotels”]; foreach($hotels as$hotel){ echo$hotel['name']。“”; foreach($key=>$value的酒店){ 如果($key==“id”){ 继续; } 如果($key==“name”){ 继续; } 回显“{$key}:{$value}”。”; } echo PHP_EOL.PHP_EOL.“”; } }否则{ echo$数据[“原因”]; } ?> 酒店名称 钥匙 价值
这是我的密码。如您所见,我正在使用curl解析一些数据,更具体地说是名称、id、位置等。。并将其存储在名为PHP解析酒店数据(名称、位置等),并使用表中的foreach循环打印它们 $curl=curl_init(); $response=curlPost($curl,$url,$headers,http_build_query($body)); $data=json_decode($response,true); 如果($data[“success”]==true){ $hotels=$data[“hotels”]; foreach($hotels as$hotel){ echo$hotel['name']。“”; foreach($key=>$value的酒店){ 如果($key==“id”){ 继续; } 如果($key==“name”){ 继续; } 回显“{$key}:{$value}”。”; } echo PHP_EOL.PHP_EOL.“”; } }否则{ echo$数据[“原因”]; } ?> 酒店名称 钥匙 价值,php,arrays,foreach,Php,Arrays,Foreach,这是我的密码。如您所见,我正在使用curl解析一些数据,更具体地说是名称、id、位置等。。并将其存储在名为data的变量中。我知道如何打印数据,但我不知道如何将数据放在下表中。例如,我想在美国的位置下面说帕里斯希尔顿酒店。。 有人帮忙吗?thnx在循环中,您可以实际回显表中的内容: $curl = curl_init(); $response = curlPost($curl, $url, $headers, http_build_query($body)); $data = json_dec
data
的变量中。我知道如何打印数据,但我不知道如何将数据放在下表中。例如,我想在美国的位置下面说帕里斯希尔顿酒店
。。
有人帮忙吗?thnx在循环中,您可以实际回显表中的内容:
$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/>
您的注释指的是常规的表呈现,这与我最初给您的没有太大区别
<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>
foreach($hotels作为$hotel){
回声“;
回声“;
回声“;
echo“{$hotel['name']}”;
回声“;
回声“;
foreach($key=>$value的酒店){
如果($key==“id”){
继续;
}
如果($key==“name”){
继续;
}
回声“;
回声“;
回显“{$key}”。{$value}”。
”;
回声“;
回声“;
}
回声“;
回声“
”;
}
}否则{
echo$数据[“原因”];
}
我知道你在那里做了什么,但你能从上到下做吗?像这样的表https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_intro
<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>
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"];
}