我有新的问题:PHP的XML - 通过它得到段的所有属性的ID
有没有办法让XML元素的所有属性,当我选择这个元素通过它的id?
我已经有了XML响应,看起来像这样:
<availResponse>
<tarifs currency="PLN">
<tarif tarifId="206844566_206844566" adtBuy="167.96" adtSell="167.96" chdBuy="167.96" chdSell="167.96" infBuy="167.96" infSell="167.96" taxMode="INCL" topCar="false" topHotel="false" adtCancel="0.0" chdCancel="0.0" infCancel="0.0" powerPricerDisplay="sell">
<fareXRefs>
<fareXRef fareId="206844566">
<flights>
<flight flightId="1663150500" addAdtPrice="0.0" addChdPrice="0.0" addInfPrice="0.0" extFlightInfo="Lowest Fare">
<legXRefs>
<legXRef legId="1981746874" class="R" cos="E" cosDescription="ECONOMY" fareBaseAdt="LOWCOST"/>
</legXRefs>
</flight>
<flight flightId="1663150499" addAdtPrice="13.0" addChdPrice="13.0" addInfPrice="13.0" extFlightInfo="Lowest Fare">
<legXRefs>
<legXRef legId="1981746874" class="R" cos="E" cosDescription="ECONOMY" fareBaseAdt="LOWCOST"/>
</legXRefs>
</flight>
</flights>
</fareXRef>
</fareXRefs>
</tarif>
</tarifs>
<legs>
<leg legId="1981746939" depApt="WAW" depDate="2014-10-09" depTime="06:20" dstApt="TXL" arrDate="2014-10-09" arrTime="07:45" equip="" fNo="8071" cr="AB" miles="0" elapsed="1.42" meals="no meals" smoker="false" stops="0" eticket="true" ocr="AB"/>
<leg legId="1981747261" depApt="WAW" depDate="2014-10-09" depTime="17:25" dstApt="CPH" arrDate="2014-10-09" arrTime="18:45" equip="CR9" fNo="2752" cr="SK" miles="414" elapsed="1.33" meals="food and beverages for purchase" smoker="false" stops="0" eticket="true" ocr="SK" seats="8"/>
<leg legId="1981747262" depApt="CPH" depDate="2014-10-09" depTime="20:10" dstApt="LHR" arrDate="2014-10-09" arrTime="21:10" equip="320" fNo="1501" cr="SK" miles="594" elapsed="2.0" meals="light lunch" smoker="false" stops="0" eticket="true" ocr="SK" seats="9"/>
<leg legId="1981747267" depApt="LHR" depDate="2014-12-09" depTime="06:40" dstApt="CPH" arrDate="2014-12-09" arrTime="09:30" equip="320" fNo="500" cr="SK" miles="594" elapsed="1.83" meals="light lunch" smoker="false" stops="0" eticket="true" ocr="SK" seats="9"/>
<leg legId="1981746874" depApt="WAW" depDate="2014-10-09" depTime="15:45" dstApt="CDG" arrDate="2014-10-09" arrTime="18:10" equip="319" fNo="1347" cr="AF" miles="0" elapsed="2.42" meals="" smoker="false" stops="0" eticket="true" ocr="AF" seats="9"/>
<leg legId="1981747268" depApt="CPH" depDate="2014-12-09" depTime="15:35" dstApt="WAW" arrDate="2014-12-09" arrTime="16:55" equip="CR9" fNo="2751" cr="SK" miles="414" elapsed="1.33" meals="food and beverages for purchase" smoker="false" stops="0" eticket="true" ocr="SK" seats="9"/>
<leg legId="1981746966" depApt="ZRH" depDate="2014-12-09" depTime="19:20" dstApt="TXL" arrDate="2014-12-09" arrTime="20:45" equip="" fNo="8199" cr="AB" miles="0" elapsed="1.42" meals="no meals" smoker="false" stops="0" eticket="true" ocr="AB"/>
<leg legId="1981747462" depApt="LHR" depDate="2014-12-09" depTime="17:50" dstApt="BRU" arrDate="2014-12-09" arrTime="20:00" equip="AR1" fNo="2096" cr="SN" miles="0" elapsed="1.17" meals="" smoker="false" stops="0" eticket="true" ocr="SN" seats="9"/>
</legs>
</availResponse>
,现在是疯狂:)
我得到的每'legId'从属性的每个tarif-> fareXRefs - > fareXRef->机票 - >机票 - > legXRefs-> legXRef
$counted = $test['cntTarifs'];
for($i=0; $i < $counted; $i++) {
$test4 = $test->tarifs->tarif[$i]->fareXRefs;
$test5 = $test4->fareXRef->flights;
$test6 = $test5->flight->legXRefs->legXRef;
$segment= $test->legs->leg;
//now we're getting every needed thing
foreach($test5 as $keyless) { //sorting each flight element
foreach($test6 as $key) { //takingout every legXRef in each flight element
$id = $key['legId']; //this is what i'm looking for
echo "<br />".$id;
foreach($segment as $seg) { //trying to find leg
if($seg['legId'] == $id){ //if legId in leg equals to $id
echo $seg['dstApt']; // try to get another attribute and that doesn't work
}
}
}
}
}
我需要让每一个贡献存在于腿与选择legId但我的思想失败:(
哈奏效魅力感谢@Ghost – arclite 2014-09-24 12:49:55
@arclite真高兴它帮助 – Ghost 2014-09-24 12:50:53
像@ ghost:只是FYI:通过xpath的属性已经可能了:http://stackoverflow.com/a/26318999/367456 – hakre 2014-10-11 20:37:04