title: PHP Arrays - Sort and Priority
body: <p>In my website, users can publish their profile in different languages, something like LinkedIn, now we're searching between firstname of users, for example we search for: '<code>ar</code>' and we look into all profiles languages, we will have an array in return like:</p>
  
  <pre><code>array
    0 =&gt; string '20=&gt;en' (length=5)
    1 =&gt; string '42=&gt;en' (length=5)
    2 =&gt; string '20=&gt;fa' (length=5)
    3 =&gt; string '42=&gt;sp' (length=5)
    4 =&gt; string '12=&gt;fr' (length=5)
    5 =&gt; string '83=&gt;ar' (length=5)
    6 =&gt; string '160=&gt;sp' (length=5)
  </code></pre>
  
  <p>The above array shows that we've got 6 profiles which matches our search '<code>ar</code>' in different languages, the above array says:</p>
  
  <pre><code>0 =&gt; Match found for User with ID = 20 in English Lang(en) profile
  1 =&gt; Match found for User with ID = 42 in English Lang(en) profile
  2 =&gt; Match found for User with ID = 20 in Farsi Lang(fa) profile
  3 =&gt; Match found for User with ID = 42 in Spanish Lang(sp) profile
  4 =&gt; Match found for User with ID = 12 in French Lang(fr) profile
  5 =&gt; Match found for User with ID = 83 in Arabic Lang(ar) profile
  6 =&gt; Match found for User with ID = 160 in Spanish Lang(sp) profile
  </code></pre>
  
  <p>Now, we want to show the results, but as a matter of fact, as you see in the results, we've got matches for '<code>ar</code>' for the user with ID = 20 in Both <code>English</code> and <code>Farsi</code> languages, but we can't show 2 results of the same person! so we need to let go of the axillary results, so the array above should be filtered and uniqued based by user IDs and priority of the languages, my priority for the languages is:</p>
  
  <ol>
  <li>$_SESSION['my_lang'];</li>
  <li>English Language (en);</li>
  <li>Rand();</li>
  </ol>
  
  <p>The person who enters '<code>ar</code>' as his search query has a <code>$_SESSION['my_lang']</code>, so in the results array for the user which has results in <code>$_SESSION['my_lang']</code> we should keep that results and let go of the other matches for the same person.</p>
  
  <p>After <code>$_SESSION['my_lang']</code>, our priority should be <strong>EN lang</strong>, if a person has results in N languages, but we could not found the match in <code>$_SESSION['my_lang']</code>, then we should keep the results in <strong>en lang</strong> and <em>clear the rest of results</em> for that person.</p>
  
  <p>after the above priorities, actually nothing matters, we just need to keep one of the results for that person and <em>get rid of other results for that person</em>, so the languages should be selected randomly...</p>
  
  <p>I have no idea how this could be accomplished,
  I would appreciate any kind of help.</p>
  
  <hr>
  
  <p>In my example, I have an array like:</p>
  
  <pre><code>array
    0 =&gt; string '20=&gt;en' (length=5)
    1 =&gt; string '42=&gt;en' (length=5)
    2 =&gt; string '20=&gt;fa' (length=5)
    3 =&gt; string '42=&gt;sp' (length=5)
    4 =&gt; string '12=&gt;fr' (length=5)
    5 =&gt; string '83=&gt;ar' (length=5)
    6 =&gt; string '160=&gt;sp' (length=5)
  </code></pre>
  
  <p>but in your example, you have an array like:</p>
  
  <pre><code>$users = array(
    array('id'=&gt; 20, 'lang'=&gt;'en'),
    array('id'=&gt; 42, 'lang'=&gt;'en'),
    array('id'=&gt; 20, 'lang'=&gt;'fa'),
    array('id'=&gt; 42, 'lang'=&gt;'sp'),
    array('id'=&gt; 12, 'lang'=&gt;'fr'),
    array('id'=&gt; 83, 'lang'=&gt;'ar'),
    array('id'=&gt; 160, 'lang'=&gt;'sp'));
  </code></pre>
  
  <p>How should I make my array to look like your array, so your codes work..? Thanks</p>