title: Creating and Echoing an Array in PHP from a Complex XML Output
body: <p>I am trying to create an array with some of the data from this XML output. Given the way the data is structured I have found it to be quite tricky. I am trying to form an array with just the <code>MECHANICAL</code> <code>description</code> data. I would want my output to look something like: </p>
  
  <p><code>Mechanical
  Engine: 2.0L DOHC 4-Cylinder TFSI
  Transmission: 8-Speed Automatic w/Tirptronic
  Full-Time All-Wheel Drive</code> and so on... </p>
  
  <p>So far I have been trying to call the mechanical descriptions like this: </p>
  
  <pre><code>&lt;?php
  
  $xml = file_get_contents('note.xml');
  $dom = new DOMDocument();
  $dom-&gt;loadXML($xml);
  
  foreach ($dom-&gt;getElementsByTagName('standard') as $standard){
    $mechdescription = $standard-&gt;getElementsByTagName('description');
    $mecharray[] = $mechdescription;
  }
  foreach ($mecharray as $displaymech){
    echo $displaymech;
  }
  
  ?&gt;   
  </code></pre>
  
  <p>Here is a portion of the XML I am trying to pull from:</p>
  
  <pre><code>    &lt;VehicleDescription xmlns="urn:description7b.services.chrome.com" country="US" language="en" modelYear="2015" bestMakeName="Audi" bestModelName="A4" bestStyleName="4dr Sdn Auto quattro 2.0T Premium" bestTrimName="Premium"&gt;
        &lt;standard&gt;
          &lt;header id="1236"&gt;MECHANICAL&lt;/header&gt;
          &lt;description&gt;Engine: 2.0L DOHC 4-Cylinder TFSI -inc: Audi valvelift system&lt;/description&gt;
          &lt;category id="1048"/&gt;
          &lt;category id="1054"/&gt;
          &lt;category id="1213"/&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1236"&gt;MECHANICAL&lt;/header&gt;
          &lt;description&gt;Transmission: 8-Speed Automatic w/Tiptronic -inc: sport program and manual shift mode&lt;/description&gt;
          &lt;category id="1130"/&gt;
          &lt;category id="1195"/&gt;
          &lt;category id="1220"/&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1236"&gt;MECHANICAL&lt;/header&gt;
          &lt;description&gt;Full-Time All-Wheel Drive&lt;/description&gt;
          &lt;category id="1041"/&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1236"&gt;MECHANICAL&lt;/header&gt;
          &lt;description&gt;Engine Oil Cooler&lt;/description&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1236"&gt;MECHANICAL&lt;/header&gt;
          &lt;description&gt;80-Amp/Hr Maintenance-Free Battery w/Run Down Protection&lt;/description&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1236"&gt;MECHANICAL&lt;/header&gt;
          &lt;description&gt;150 Amp Alternator&lt;/description&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1236"&gt;MECHANICAL&lt;/header&gt;
          &lt;description&gt;Gas-Pressurized Shock Absorbers&lt;/description&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1236"&gt;MECHANICAL&lt;/header&gt;
          &lt;description&gt;Front And Rear Anti-Roll Bars&lt;/description&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1236"&gt;MECHANICAL&lt;/header&gt;
          &lt;description&gt;Electric Power-Assist Speed-Sensing Steering&lt;/description&gt;
          &lt;category id="1084"/&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1236"&gt;MECHANICAL&lt;/header&gt;
          &lt;description&gt;16.1 Gal. Fuel Tank&lt;/description&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1236"&gt;MECHANICAL&lt;/header&gt;
          &lt;description&gt;Single Stainless Steel Exhaust&lt;/description&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1236"&gt;MECHANICAL&lt;/header&gt;
          &lt;description&gt;Multi-Link Front Suspension w/Coil Springs&lt;/description&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1236"&gt;MECHANICAL&lt;/header&gt;
          &lt;description&gt;Multi-Link Rear Suspension w/Coil Springs&lt;/description&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1236"&gt;MECHANICAL&lt;/header&gt;
          &lt;description&gt;4-Wheel Disc Brakes w/4-Wheel ABS, Front Vented Discs, Brake Assist, Hill Hold Control and Electric Parking Brake&lt;/description&gt;
          &lt;category id="1018"/&gt;
          &lt;category id="1020"/&gt;
          &lt;category id="1228"/&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1176"&gt;EXTERIOR&lt;/header&gt;
          &lt;description&gt;Wheels: 8.0J x 17" 10-Spoke-Star-Design&lt;/description&gt;
          &lt;category id="1123"/&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1176"&gt;EXTERIOR&lt;/header&gt;
          &lt;description&gt;Tires: P245/45R17 AS&lt;/description&gt;
          &lt;category id="1092"/&gt;
          &lt;category id="1097"/&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1176"&gt;EXTERIOR&lt;/header&gt;
          &lt;description&gt;Wheels w/Silver Accents&lt;/description&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1176"&gt;EXTERIOR&lt;/header&gt;
          &lt;description&gt;Steel Spare Wheel&lt;/description&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1176"&gt;EXTERIOR&lt;/header&gt;
          &lt;description&gt;Compact Spare Tire Mounted Inside Under Cargo&lt;/description&gt;
          &lt;category id="1098"/&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1176"&gt;EXTERIOR&lt;/header&gt;
          &lt;description&gt;Clearcoat Paint&lt;/description&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1176"&gt;EXTERIOR&lt;/header&gt;
          &lt;description&gt;Express Open/Close Sliding And Tilting Glass 1st Row Sunroof w/Sunshade&lt;/description&gt;
          &lt;category id="1069"/&gt;
          &lt;category id="1132"/&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1176"&gt;EXTERIOR&lt;/header&gt;
          &lt;description&gt;Body-Colored Front Bumper&lt;/description&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1176"&gt;EXTERIOR&lt;/header&gt;
          &lt;description&gt;Body-Colored Rear Bumper&lt;/description&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1176"&gt;EXTERIOR&lt;/header&gt;
          &lt;description&gt;Rocker Panel Extensions&lt;/description&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
       &lt;standard&gt;
          &lt;header id="1176"&gt;EXTERIOR&lt;/header&gt;
          &lt;description&gt;Aluminum Side Windows Trim and Black Front Windshield Trim&lt;/description&gt;
          &lt;styleId&gt;369101&lt;/styleId&gt;
          &lt;installed cause="BaseEquipment"/&gt;
       &lt;/standard&gt;
   &lt;/VehicleDescription&gt;
  </code></pre>
  
  <p>Any help would be greatly appreciated. </p>
  
  <p><strong>UPDATE 1</strong><br>
  I have determined how to receive all <code>description</code>s for <code>standard</code> tags. However, I only want <code>description</code>'s for <code>MECHANICAL</code> data. Here is my current code which follows my past logic. Can I write an <code>IF</code> statement around this?</p>
  
  <pre><code>foreach ($dom-&gt;getElementsByTagName('standard') as $standard){
    $mechdescription = $standard-&gt;getElementsByTagName('description')-&gt;item(0)-&gt;nodeValue;
    $mecharray[] = $mechdescription;
  }
  foreach ($mecharray as $displaymech){
    echo $displaymech."&lt;br&gt;";
  }
  </code></pre>
  
  <p><strong>UPDATE 2</strong><br>
  @splash58 has suggested using xPath is best way to tackle this. With his guidance this is my current code. I am still not receiving any response. </p>
  
  <pre><code>&lt;?php
  
   $xml = file_get_contents('note.xml');
   $dom = new DOMDocument();
   $dom-&gt;loadXML($xml);
   $xpath = new domXpath($dom);
  
   foreach ($xpath-&gt;query('//standard[header="MECHANICAL"]/description') as $mechdescription){
   $mecharray[] = $mechdescription-&gt;nodeValue ;
  }
   foreach ($mecharray as $mechanicaldescription){
   echo $mechanicaldescription;
  }
  
  ?&gt;
  </code></pre>