mardi 26 février 2019

Creating a New Variable Based On a Matched Array ID Value

I am extracting XML file data using DOMdocument and foreach loops. The XML file data contains tag names, ids, node values and attributes all which are used to extract the data I need.

I am struggling with using an if statement to define a variable that I want to echo. The premise of the if statement is if a certain id equals a certain id number then a new variable will be equal to the node value of that id number. I've put ** ** around the two or three lines of code that are the point of issue. They are at the bottom of the PHP code.

The if statement has three possible id values (only one will match) and the node value data of the matched id should be set equal to the new variable. If the <category id="1104"/> then the node value from <category id="1104">6-Speed A/T</category> will equal the new variable $transmissionfinal. Thus, $transmissionfinal should equal 6-Speed A/T. Whereas currently, it equals A/T.

Any help is appreciated!

Here is the XML data followed by my code:

XML 1

<factoryOption standard="false" optionKindId="7" utf="C" fleetOnly="false">
   <header id="1379">TRANSMISSION</header>
   <description>TRANSMISSION, 6-SPEED AUTOMATIC FOR RWD</description>
   <oem-description sequence="1">6-speed automatic transmission</oem-description>
   <oem-description sequence="2"><![CDATA[With the 6.2L engine on your CTS<em>-V<sup>&nbsp; </sup></em>Wagon, the 6-speed automatic transmission&nbsp;provides smooth, seamless transitions throughout its gear range, perfectly complementing the luxurious feel and responsive steering of a RWD vehicle. Thanks to Performance Algorithm Shifting, you can change the shift points simply by sliding the lever, enjoying a more aggressive progression for an exhilarating experience during sustained performance driving. And, with finger-actuated paddle shift controls on the steering wheel, this automatic immediately converts into a manual.]]></oem-description>
   <category id="1043"/>
   <category id="1104"/>
   <category id="1130"/>
   <category id="1131" removed="true"/>
   <price unknown="false" invoiceMin="0.0" invoiceMax="0.0" msrpMin="0.0" msrpMax="0.0"/>
   <styleId>331824</styleId>
   <installed cause="OptionCodeBuild"/>
</factoryOption>

XML 2

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
 <S:Body>
  <CategoryDefinitions xmlns="urn:description7b.services.chrome.com">
     <responseStatus responseCode="Successful" description="Successful"/>
      <category>
         <group id="6">Powertrain</group>
         <header id="15">Transmission</header>
         <category id="1104">6-Speed A/T</category>
         <type id="15">Transmission</type>
      </category>
      <category>
         <group id="6">Powertrain</group>
         <header id="37">Drivetrain</header>
         <category id="1043">Rear Wheel Drive</category>
         <type id="3">Drivetrain</type>
      </category>
      <category>
         <group id="6">Powertrain</group>
         <header id="15">Transmission</header>
         <category id="1130">A/T</category>
         <type id="16">Transmission - Type</type>
      </category>
  </CategoryDefinitions>
 </S:Body>
</S:Envelope>

PHP CODE

<?php


$xml = file_get_contents('XML/XML 1');
$dom = new DOMDocument();
$dom->loadXML($xml);

//Category XML
$txml = file_get_contents('XML/XML 2');
$tom = new DOMDocument();
$tom->loadXML($txml);

foreach ($tom->getElementsByTagName('category') as $catty){
  foreach($catty->getElementsByTagName('category') as $cattydef){
    $cattyid = $cattydef->getAttribute("id");
    $cattyvalue = $cattydef->nodeValue;
    $cattyarray[$cattyid] = $cattyvalue;
   }
  }

foreach ($dom->getElementsByTagName('factoryOption') as $factoryopt){
 foreach($factoryopt->getElementsByTagName('category') as $factcategory){
   if ($factoryopt->getElementsByTagName('header')->item(0)->getAttribute('id') == '1379'){
     $todescription = $factoryopt->getElementsByTagName('description')->item(0)->nodeValue;
   if($factcategory->getAttribute('removed')=='true'){
       $tocategory = "";
     }
     else{
       $tocategory = $factcategory->getAttribute('id');
     }
     $toarray[$tocategory] = $todescription;
  }
 }
}

foreach ($toarray as $tocatid => $tomeat){
  foreach($cattyarray as $tcid => $tcmeat){
    if($tocatid == $tcid){
      $toarray[$tocatid] = $tcmeat;
    **if ($tocatid == ('1104' || '1105' || '1106')){
      $transmissionfinal = $toarray[$tocatid];**
    }
   }
  }
 }
echo $transmissionfinal;


echo "<br><br>";


?>

Aucun commentaire:

Enregistrer un commentaire