Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中从JSON准备表格式_Php_Json - Fatal编程技术网

如何在PHP中从JSON准备表格式

如何在PHP中从JSON准备表格式,php,json,Php,Json,我有JSON字符串,它有nitem个数,我想把它转换成一个表 Json字符串 {"HotelSearchResult":{"ResponseStatus":1,"Error":{"ErrorCode":0,"ErrorMessage":""},"TraceId":"70e718ad-a9c2-4e82-9125-e3ee2f1573f1","CityId":"15254","CheckInDate":"2016-03-22","CheckOutDate":"2016-03-24","Prefer

我有JSON字符串,它有
n
item个数,我想把它转换成一个表

Json字符串

{"HotelSearchResult":{"ResponseStatus":1,"Error":{"ErrorCode":0,"ErrorMessage":""},"TraceId":"70e718ad-a9c2-4e82-9125-e3ee2f1573f1","CityId":"15254","CheckInDate":"2016-03-22","CheckOutDate":"2016-03-24","PreferredCurrency":"INR","NoOfRooms":1,"RoomGuests":[{"NoOfAdults":1,"NoOfChild":0,"ChildAge":[]}],"HotelResults":[{"ResultIndex":1,"HotelCode":"348043","HotelName":"Base Taupo - Hostel \/ Backpacker","HotelCategory":"","StarRating":2,"HotelDescription":"Property Location A stay at Base Taupo - Hostel \/ Backpacker places you in the heart of Taupo, walking distance from Taupo Museum and Art Gallery and Barbary.  This hostel is within Near Taupo Museum and Art Gallery  ","HotelPromotion":"","HotelPolicy":"","Price":{"CurrencyCode":"INR","RoomPrice":1837.47,"Tax":0.00,"ExtraGuestCharge":0,"ChildCharge":0,"OtherCharges":0,"Discount":0.00,"PublishedPrice":1837.47,"PublishedPriceRoundedOff":1837,"OfferedPrice":1837.47,"OfferedPriceRoundedOff":1837,"AgentCommission":0.00,"AgentMarkUp":0.00,"ServiceTax":0,"TDS":0},"HotelPicture":"http:\/\/www.travelboutiqueonline.com\/imageresource.aspx?img=ckZZ2jR\/KJFL8uCNV\/6rUdikLJoSoWwsQ4+ucla4wpAL9fwTuqkLXc3dJ9jA0ZdlXkihFvi968p+KSfi8oYVU8Tj278FBz\/1XkfoRZtaubj\/jenFXq1xBUpWskdqQlXdlSq2E+AsRgAHCH7MUGE+VOR1GoL89wEcl7bKkOpOKoV8u5\/s6kQwUbyMvkUTHXfr\/OjXIi4RNZfESY3+adSUVfiu7NVoTmH5TlFdNsylMzU=","HotelAddress":"7 Tuwharetoa Street, Taupo, , 3330, , , ","HotelContactNo":"","HotelMap":null,"Latitude":"","Longitude":"","HotelLocation":null,"SupplierPrice":null,"RoomDetails":[]},{"ResultIndex":2,"HotelCode":"444277","HotelName":"Camellia Court Family Motel","HotelCategory":"","StarRating":3,"HotelDescription":"Property Location Located in Taupo, Camellia Court Family Motel is convenient to Taupo Bungy and Taupo Museum and Art Gallery.  This motel is within close proximity of Barbary and Near Taupo Bungy  ","HotelPromotion":"","HotelPolicy":"","Price":{"CurrencyCode":"INR","RoomPrice":2721.38,"Tax":0.00,"ExtraGuestCharge":0,"ChildCharge":0,"OtherCharges":0,"Discount":0.00,"PublishedPrice":2721.38,"PublishedPriceRoundedOff":2721,"OfferedPrice":2721.38,"OfferedPriceRoundedOff":2721,"AgentCommission":0.00,"AgentMarkUp":0.00,"ServiceTax":0,"TDS":0},"HotelPicture":"http:\/\/www.travelboutiqueonline.com\/imageresource.aspx?img=ckZZ2jR\/KJFL8uCNV\/6rUdikLJoSoWwsQ4+ucla4wpAL9fwTuqkLXc3dJ9jA0ZdlXkihFvi968p+KSfi8oYVU8Tj278FBz\/1XkfoRZtaubj\/jenFXq1xBQF2KZhLrrzlriBhuyokbu67Iabfrn\/oYXyyOQZ\/9ufWgRFwVHZjjFr3Cndi8RUX2MpUZp2C+OI\/3j7hNbZEVxU3aohoWZK3Xd79WBVqb\/DrXZotWZd4wAU=","HotelAddress":"50 Tonga Street, Taupo, , 2730, , , ","HotelContactNo":"","HotelMap":null,"Latitude":"","Longitude":"","HotelLocation":null,"SupplierPrice":null,"RoomDetails":[]},}};
必须查看完整的数据


所需的HTML输出:

使用json_decode获取PHP数组。将下面的代码(替换为json字符串)粘贴到localhost php文件中,并在浏览器中查看结果


使用json_解码

$jsonString = '{"HotelSear.....';

$data = json_decode($jsonString);

foreach($data as $k=>$v) {
    echo "key [$k] => value [$v]\n";
}
然后您将拥有一个数组(在可能的情况下具有关联索引),您可以使用一个简单的foreach循环运行数据

$json = '{"HotelSearchResult...';    

echo "<table>";
$data = json_decode($json, true);
foreach ($data['HotelSearchResult']['HotelResults'] as $hotelResult) {
    echo "<tr>
        <td>{$hotelResult['HotelName']}</td>
        <td>{$hotelResult['Price']['RoomPrice']}</td>
    </tr>";
}
echo "</table>";

您应该使用
json\u decode
对json数据进行解码,而不仅仅是在结构上循环

<?php
    $json='{"HotelSearchResult":value......};
    echo "<table border='1'>";
    echo '<thead><tr><td>Hotel Name </td><td>Price</td><td>Action</td></tr></thead>';
    echo "<tbody>";
    $str = json_decode($json, true);
    foreach ($str['HotelSearchResult']['HotelResults'] as $key=> $Result) {
        echo "<tr><td>".$Result['HotelName']."</td>
            <td>".$Result['Price']['RoomPrice']."</td>
            <td><button type='button' value=".$Result['ResultIndex']." >Select</button></td>
        </tr>";
    }
    echo"</tbody>";
    echo "</table>";
$json='{“HotelSearchResult…”;
回声“;
$data=json_decode($json,true);
foreach($HotelSearchResult'数据]['HotelResults']作为$hotelResult){
回声“
{$hotelResult['HotelName']}
{$hotelResult['Price']['RoomPrice']}
";
}
回声“;
你应该试试下面这是我的代码


这就是您要找的

$json='{“HotelSearchResult”:{“ResponseStatus”:1,“Error”:{“ErrorCode”:…};
echo“酒店名称价格行动”;
$str=json_decode($json,true);
foreach($str['HotelSearchResult']['HotelResults']作为$key=>$Result){
回显“.$Result['HotelName']”
“$Result['Price']['RoomPrice']”
挑选
";
}
回声';

将您的
JSON
字符串转换为
数组
并简单地循环输出答案应避免过度依赖外部链接;它们应该是自包含的,尽管外部链接可以用作参考。请添加更多详细信息。
<?php
    $json='{"HotelSearchResult":value......};
    echo "<table border='1'>";
    echo '<thead><tr><td>Hotel Name </td><td>Price</td><td>Action</td></tr></thead>';
    echo "<tbody>";
    $str = json_decode($json, true);
    foreach ($str['HotelSearchResult']['HotelResults'] as $key=> $Result) {
        echo "<tr><td>".$Result['HotelName']."</td>
            <td>".$Result['Price']['RoomPrice']."</td>
            <td><button type='button' value=".$Result['ResultIndex']." >Select</button></td>
        </tr>";
    }
    echo"</tbody>";
    echo "</table>";
$json='{"HotelSearchResult":{"ResponseStatus":1,"Error":{"ErrorCode":...};
echo '<table><thead><tr><td>Hotel Name </td><td>Price</td><td>Action</td></tr></thead><tbody>';
$str = json_decode($json, true);
foreach ($str['HotelSearchResult']['HotelResults'] as $key=> $Result) {
    echo "<tr><td>".$Result['HotelName']."</td>
        <td>".$Result['Price']['RoomPrice']."</td>
        <td><button type='button' value=".$Result['ResultIndex']." >Select</button></td>
    </tr>";
}
echo '</tbody></table>';