以下是我使用 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" } }