PHP XML - get all attributes of segment by it's id -


i've got new question:

is there way attributes of xml element when i'm selecting element it's id ?

i've got xml response looks this:

<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 , beverages 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 , beverages 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> 

and it's madness :)

i'm getting every 'legid' attribute each tarif->farexrefs->farexref->flights->flight->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 $keyless) { //sorting each flight element     foreach($test6 $key) { //takingout every legxref in each flight element         $id = $key['legid']; //this i'm looking          echo "<br />".$id;          foreach($segment $seg) { //trying find leg             if($seg['legid'] == $id){ //if legid in leg equals $id                 echo $seg['dstapt']; // try attribute , doesn't work             }         }     }  } } 

i need every attribute exists in leg selected legid mind fails :(

here way in simplexml has support in xpath query elements , attributes.

so following code select attributes of <leg> element has specific value in it's legid attribute.

then $data array built based on attribute names , values:

$xml        = simplexml_load_string($buffer); $legid      = '1981746939'; $attributes = $xml->xpath(sprintf("(//leg[@legid=%d])[1]/@*", $legid)); $data       = null; foreach ($attributes $attribute) {     $data[$attribute->getname()] = (string)$attribute; } print_r($data); 

result:

array (     [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 ) 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -