title: Associative array - move an element to first position
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/5783750/php-add-item-to-beginning-of-associative-array">PHP - add item to beginning of associative array</a>  </p>
  </blockquote>
  
  
  
  <p>have associative array:</p>
  
  <pre><code>$myarray=array("key1"=&gt;"value1","key2"=&gt;"value2");
  </code></pre>
  
  <p>Need to add a new element to array:</p>
  
  <pre><code>$myarray["keynew"]="valuenew";
  </code></pre>
  
  <p>So now $myarray is:</p>
  
  <pre><code>Array ( [key1] =&gt; value1 [key2] =&gt; value2 [keynew] =&gt; valuenew ) 
  </code></pre>
  
  <p>How can I move the new added element ("keynew"=>"valuenew") to the first position of the array?</p>
  
  <p>I have an workaround, but I don't like it.</p>
  
  <pre><code>$myarray=array("key1"=&gt;"value1","key2"=&gt;"value2");
  $myarray["keynew"]="-valuenew";
  asort($myarray);
  $myarray["keynew"]="valuenew";
  //$myarray=Array ( [keynew] =&gt; valuenew [key1] =&gt; value1 [key2] =&gt; value2 ) 
  </code></pre>
  
  <p>Thank you.</p>