基于匹配的数组ID值创建新变量

时间:2019-02-26 19:22:43

标签: php xml if-statement foreach domdocument

我正在使用DOMdocumentforeach循环提取XML文件数据。 XML文件数据包含tag namesidsnode valuesattributes,所有这些都用于提取我需要的数据。

我正在努力使用if statement来定义要回显的变量。 if statement的前提是,如果某个id等于某个id number,则新变量将等于该node value的{​​{1}}。我已经把两三行代码放在** **了,这是问题所在。它们位于PHP代码的底部。

id number具有三个可能的id值(仅一个将匹配),匹配ID的if statement数据应设置为等于新变量。如果node value,则<category id="1104"/>中的节点值将等于新变量<category id="1104">6-Speed A/T</category>。因此,$transmissionfinal应该等于 6-Speed A / T 。目前,它等于 A / T

感谢您的帮助!

这是XML数据,后面是我的代码:

XML 1

$transmissionfinal

XML 2

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

PHP代码

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

0 个答案:

没有答案