0

以下是我使用 simpleXML 解析的 XML 响应的一部分。我需要创建一个如下例所示的表格,但我不确定如何实现这一点。

$roomCategory ='
<ChargeConditions>
 <ChargeCondition Type="cancellation">
  <Condition Charge="true" ChargeAmount="2730.00" Currency="USD" FromDay="0" ToDay="0"/>
  <Condition Charge="true" ChargeAmount="390.00" Currency="USD" FromDay="1" ToDay="2"/>
  <Condition Charge="false" FromDay="3"/>
 </ChargeCondition>
 <ChargeCondition Type="amendment">
  <Condition Charge="false" FromDay="0"/>
 </ChargeCondition>
</ChargeConditions>';

所需表

 __________________________________________________
|____________|_____Cancelation____|___Ammendment___|
|  FromDay 0 |      2730.00       |   No Charge    |
|  ToDay 0   |                    |                |
|____________|____________________|________________|
|  FromDay 1 |       390.00       |   No Charge    |
|  ToDay 2   |                    |                |
|____________|____________________|________________|
|  FromDay 3 |      No Charge     |   No Charge    |
|            |                    |                |
|____________|____________________|________________|

我遇到了麻烦,因为修改条件来自不同的节点,我正在尝试做这样的事情,但我不确定我是否走在正确的轨道上,以及如何回显所有内容以形成上表。

更新代码:

$chargeConditions = $roomCategory->ChargeConditions;
echo '<table>';
echo '<tr>';
echo '<th></th>';
foreach ($chargeConditions->ChargeCondition as $condition) {
  echo '<th>' . ucfirst($condition['Type']) . '</th>';
}
echo '</tr>';
echo '<tr>';
$val = array();
$c = 0;
foreach ($chargeConditions->ChargeCondition as $condition) {
  $val[$c][0] = $condition["Type"];
  $charges = $condition->Condition;
  var_dump($charges);
  foreach($charges as $charge){
    if($charge["Charge"] !== "false"){
   $val[$c][1] = $charge["FromDay"];
   $val[$c][2] = $charge["ToDay"];
   $val[$c][3] = $charge["ChargeAmount"];
     }
  }
   $c++;
 }

for($x=0; $x <= $c; $x++){
 echo '<tr>';
 echo '<td>From Day' . $val[$x][1] .'<br>To Day' . $val[$x][2] . '</td>';
 echo '<td>' . $val[$x][3] . '</td>';
 echo '<td>' . $val[$x+1][3] . '</td>';
 echo '</tr>';
}
echo '</table>';

上面的代码生成了一个类似于这个的表:

 __________________________________________________
|____________|_____Cancelation____|___Ammendment___|
|  FromDay 3 |                    |                |
|  ToDay     |                    |                |
|____________|____________________|________________|
|  FromDay 0 |                    |                |
|  ToDay     |                    |                |
|____________|____________________|________________|
|  FromDay   |                    |                |
|  ToDay     |                    |                |
|____________|____________________|________________|

var_dump($charges) 给出以下输出:

object(SimpleXMLElement)#455 (1) { ["@attributes"]=> array(5) { ["Charge"]=> string(4) "true" ["ChargeAmount"]=> string(7) "2730.00" ["Currency"]=> string(3) "USD" ["FromDay"]=> string(1) "0" ["ToDay"]=> string(1) "0" } } object(SimpleXMLElement)#504 (1) { ["@attributes"]=> array(2) { ["Charge"]=> string(5) "false" ["FromDay"]=> string(1) "0" } }
4

1 回答 1

0

我会将所有内容解析为一个数组,其键为 FromDay ......类似于

$cancellation=array();
$amendment=array();
foreach ($chargeConditions->ChargeCondition as $condition) {
$var = $condition->attributes()->Type;   
    foreach($condition as $child)
    {
         $$var[$child->attributes()->FromDay]=($child->attributes()->Charge)?$child->attributes()->ChargeAmount:"No Charge";
    }

}

一旦阵列就位,只需执行另一个循环。

<table><tr><td>&nbsp;</td><td>Cancel</td><td>Ammend</td></tr>
<?php
foreach ($cancellations as $fromDay=>$cancel)
{
?>
<tr>
<td><?php echo $fromDay?></td>
<td><?php echo $cancel?></td>
<td><?php echo isset($ammendment[$fromDay])?$ammendment[$fromDay]:"No Charge"?></td>
</tr>
<?php
}
?>
</table>
于 2013-11-10T04:29:35.437 回答