まずコードはこちら
料金.xml
<?xml version="1.0" encoding="UTF-8"?>
<main>
<unittype id="24944">
<date value="2020-10-10">
<baserate>45.50</baserate>
<closed>0</closed>
<unitsavailable>2</unitsavailable>
</date>
</unittype>
<unittype id="24944">
<date value="2020-10-11" value2="2020-10-13">
<baserate>99</baserate>
<minimumstay>2</minimumstay>
<unitsavailable>9</unitsavailable>
</date>
<date from="2020-10-15" to="2020-10-20">
<closed>1</closed>
</date>
</unittype>
<unittype id="24946">
<date value="2020-10-10">
<unitsavailable>0</unitsavailable>
<closed>1</closed>
</date>
</unittype>
</main>
ここにindex.phpがあります
<?php
$xml = simplexml_load_file("xml_files/rates.xml");
//$xml = new SimpleXMLElement($xml);
$room_id = 24944;
$date = "2020101";
$hotel = $xml -> xpath('/main/unittype[@id = "'.$room_id.'"]/date[number(translate(@value,\'-\',\'\')) >"' .$date.'" ]');
var_dump($hotel);
foreach($hotel as $room) {
//var_dump($room);
echo "<br /><br />";
echo "The room with the ID: {$room->unittype} <br />";
echo "Date: {$room[0]->attributes()['value']}<br />";
echo "Room Price: {$room->baserate}<br />
Room min stay: {$room->unitsavailable}<br />";
}
?>
$room->unittypeのように、選択された部屋でユニットタイプのIDにアクセスしようとしていますが、もちろんうまくいきません。誰もこれを解決する方法を知っていますか?
var_dump($hotel) の出力は
array (size=2)
0 =>
object(SimpleXMLElement)[2]
public '@attributes' =>
array (size=1)
'value' => string '2020-10-10' (length=10)
public 'baserate' => string '45.50' (length=5)
public 'closed' => string '0' (length=1)
public 'unitsavailable' => string '2' (length=1)
1 =>
object(SimpleXMLElement)[3]
public '@attributes' =>
array (size=2)
'value' => string '2020-10-11' (length=10)
'value2' => string '2020-10-13' (length=10)
public 'baserate' => string '99' (length=2)
public 'minimumstay' => string '2' (length=1)
public 'unitsavailable' => string '9' (length=1)