title: PHP: How to sort XML alphabetically by attribute before "for each" loop
body: <h2>The setup:</h2>
  
  <p>I have an external XML file I have no control over, here is it:</p>
  
  <h3>001.xml</h3>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Gallery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ID="001" Name="001"&gt;
  &lt;Images&gt;
  &lt;ImageGalleryEntry Name="1 John" Description="Doe" FileName="830358.1john.png" Width="100" Height="100"&gt;
  &lt;S3Key&gt;accounts/b2d174c1-a408-4d58-9d48-1490ac4e9f9b/site-37857/cms-assets/images/830358.logooraclehealthsciencesthumb.png&lt;/S3Key&gt;
  &lt;S3ThumbnailKey&gt;accounts/b2d174c1-a408-4d58-9d48-1490ac4e9f9b/site-37857/cms-assets/thumbnails/830358.logooraclehealthsciencesthumb.png&lt;/S3ThumbnailKey&gt;
  &lt;ContentLength&gt;1309&lt;/ContentLength&gt;
  &lt;/ImageGalleryEntry&gt;
  &lt;ImageGalleryEntry Name="11 Jane" Description="Doet" FileName="943702.11jane.png" Width="100" Height="100"&gt;
  &lt;S3Key&gt;accounts/b2d174c1-a408-4d58-9d48-1490ac4e9f9b/site-37857/cms-assets/images/943702.logorochethumb.png&lt;/S3Key&gt;
  &lt;S3ThumbnailKey&gt;accounts/b2d174c1-a408-4d58-9d48-1490ac4e9f9b/site-37857/cms-assets/thumbnails/943702.logorochethumb.png&lt;/S3ThumbnailKey&gt;
  &lt;ContentLength&gt;1403&lt;/ContentLength&gt;
  &lt;/ImageGalleryEntry&gt;
  &lt;ImageGalleryEntry Name="10 Jack" Description="Smith" FileName="965501.10jack.png" Width="100" Height="100"&gt;
  &lt;S3Key&gt;accounts/b2d174c1-a408-4d58-9d48-1490ac4e9f9b/site-37857/cms-assets/images/965501.pwclogo.png&lt;/S3Key&gt;
  &lt;S3ThumbnailKey&gt;accounts/b2d174c1-a408-4d58-9d48-1490ac4e9f9b/site-37857/cms-assets/thumbnails/965501.pwclogo.png&lt;/S3ThumbnailKey&gt;
  &lt;ContentLength&gt;7021&lt;/ContentLength&gt;
  &lt;/ImageGalleryEntry&gt;
  &lt;/Images&gt;
  &lt;/Gallery&gt;
  </code></pre>
  
  <p>I also have a php code that takes this info and puts it out in html nicely:</p>
  
  <h3>index.php fragment</h3>
  
  <pre><code>&lt;?php
  $url = '001.XML';
  $xml = simplexml_load_file($url);
  foreach($xml-&gt;Images-&gt;ImageGalleryEntry as $item) {
      echo $item['FileName'] . "&lt;br/&gt;" . $item['Name'] . "&lt;br/&gt;" . $item['Description'] . "&lt;br/&gt;&lt;br/&gt;";
  };
  ?&gt;
  </code></pre>
  
  <h3>The output is:</h3>
  
  <blockquote>
    <blockquote>
      <p>830358.1john.png<br/>
      1 John<br/>
      Doe<br/>
      <br/>
      943702.11jane.png<br/>
      11 Jane<br/>
      Doet<br/>
      <br/>
      965501.10jack.png<br/>
      10 Jack<br/>
      Smith<br/></p>
    </blockquote>
  </blockquote>
  
  <p>The problem is that I actually need it ordered by the Name attribute, like this:</p>
  
  <blockquote>
    <blockquote>
      <p>830358.1john.png<br/>
      1 John<br/>
      Doe<br/>
      <br/>
      965501.10jack.png<br/>
      10 Jack<br/>
      Smith<br/>
      <br/>
      943702.11jane.png<br/>
      11 Jane<br/>
      Doet<br/></p>
    </blockquote>
  </blockquote>
  
  <p>Note that the XML constantly changes, so the php should automatically order them alphabetically by Name. I'm guessing I need to somehow sort alphabetically all the ImageGalleryEntry tags by their Name attribute before entering the for each loop, but no clue how to do that.</p>
  
  <p>Please provide an actual way to do it, I'm new to php, and won't know what to do if you say something like "make an array".</p>