0

这是代码

费率.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 />";
}
?>

现在我正在尝试使用所选房间获取单元类型中的 ID,如 $room->unittype 但当然它不起作用所以有人知道如何解决这个问题吗?

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)
4

2 回答 2

1
$room->unittype->attributes()->id;

参考

于 2012-08-03T00:56:48.973 回答
0

我明白了,伙计们。非常感谢,对不起,如果我打扰了任何人,这是解决方案以防万一其他人需要它

<?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 />";
    $node_name = $room->getName();
    //echo $node_name."<br/><br/>";
    $parent = $room->xpath("parent::*");
    //print_r($parent);
    echo "The room with the ID: {$parent[0]->attributes()['id']} <br />";
    echo "Date: {$room[0]->attributes()['value']}<br />";
    echo "Room Price: {$room->baserate}<br />
    Room min stay: {$room->unitsavailable}<br />";
}
?>
于 2012-08-03T01:27:39.143 回答