title: PHP: Dynamically sort a multi-dimensional array
body: <p>I have a function common to sites based in different locations. The two-dimensional array looks something like this:</p>
  
  <pre><code>$sites = array(
      'UK' =&gt; array(
          "sitecode" =&gt; "AUTest",
          "domain" =&gt; ".com.au",
          "label" =&gt; "Australia",
          ), 
      'DE' =&gt; array(
          "sitecode" =&gt; "DETest",
          "domain" =&gt; ".de",
          "label" =&gt; "Germany"
          ), 
      'CA' =&gt; array(
          "sitecode" =&gt; "CATest",
          "domain" =&gt; ".ca",
          "label" =&gt; "Canada"
          )
  );
  </code></pre>
  
  <p>I'd like to display a list of all countries on each site, but always display the current country at the top of the list, with the others listed in their natural order, e.g.</p>
  
  <p>In Australia, I want to see:</p>
  
  <pre><code>&lt;li class="foo"&gt;&lt;a href="http://example.com.au"&gt;Australia&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://example.de"&gt;Germany&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://example.ca"&gt;Canada&lt;/a&gt;&lt;/li&gt;
  </code></pre>
  
  <p>In Germany:</p>
  
  <pre><code>&lt;li class="foo"&gt;&lt;a href="http://example.de"&gt;Germany&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://example.com.au"&gt;Australia&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://example.ca"&gt;Canada&lt;/a&gt;&lt;/li&gt;
  </code></pre>
  
  <p>In Canada:</p>
  
  <pre><code>&lt;li class="foo"&gt;&lt;a href="http://example.ca"&gt;Canada&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://example.com.au"&gt;Australia&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://example.de"&gt;Germany&lt;/a&gt;&lt;/li&gt;
  </code></pre>
  
  <p>If I have something like this:</p>
  
  <pre><code>foreach ($sites as $key =&gt; $list) {  
      if (My_External_Variable ==  $list['sitecode']) { 
          echo '&lt;li&gt;&lt;a class="foo" href="http://www.example' . $list['domain'] .'/"&gt;' . $list['label'] . '&lt;/a&gt;';
      } 
      else { 
          echo '&lt;li&gt;&lt;a href="http://www.example' . $list['domain'] . '"&gt;' . $list['label']  . '&lt;/a&gt;&lt;/li&gt;'; 
      } 
  }
  </code></pre>
  
  <p>I can find the current country site I'm viewing and, for instance, apply a css class, but as it is, it's always going to display in it's index order. </p>
  
  <p>I've looked at a lot of sort man pages, but have to confess I'm not a clever man. I <em>think</em> what I need to do is dynamically change the index based on the country site I'm currently viewing so that I can place it first, then iterate through the remaining. I'm sure it can be done, but am stuck trying to make it so.</p>
  
  <p>I'm hoping someone can point me in the right direction.</p>