title: Multidimensional array sort and unique by key- PHP
body: <pre><code>$mp = array(
  array('url'=&gt;'www.abc.com','time'=&gt;'1433551154','referral_url'=&gt;'www.pqr.com'),
  array('url'=&gt;'www.xyz.com','time'=&gt;'1433551150','referral_url'=&gt;'www.stu.com'),
  array('url'=&gt;'www.lmn.com','time'=&gt;'1433551190','referral_url'=&gt;'www.jhi.com'),
  array('url'=&gt;'www.rst.com','time'=&gt;'1433551170','referral_url'=&gt;'www.pqr.com')
  );
  </code></pre>
  
  <p>The above is the input array, I need output with sort by time and unique by referral_url.</p>
  
  <p>Resultant will be-</p>
  
  <pre><code>$mp = array(
  array('url'=&gt;'www.lmn.com','time'=&gt;'1433551190','referral_url'=&gt;'www.jhi.com'),
  array('url'=&gt;'www.rst.com','time'=&gt;'1433551170','referral_url'=&gt;'www.pqr.com'),
  array('url'=&gt;'www.lmn.com','time'=&gt;'1433551150','referral_url'=&gt;'www.stu.com'),
  );
  </code></pre>
  
  <p>Remember first sort by time then remove the duplicate by referral_url.</p>
  
  <p>My Code, what i tried-</p>
  
  <pre><code>public function unique_sort($arrs, $id) {
      $unique_arr = array();
      foreach ($arrs AS $arr) {
  
          if (!in_array($arr[$id], $unique_arr)) {
              $unique_arr[] = $arr[$id];
          }
      }
      sort($unique_arr);
      return $unique_arr;
  } 
  foreach($mp as $key =&gt; $row){
          $referral_url[$key]  = $row['referral_url'];
          $time[$key] = $row['time'];
          $url[$key] = $row['url'];
      }
  array_multisort($time, SORT_DESC, $mp);
  $sort_arr = $this-&gt;unique_sort($mp, 'referral_url');
  print_R($uniqueArray);exit;
  </code></pre>
  
  <p>But it only result me referral_url-</p>
  
  <pre><code>Array(
  [0] =&gt; www.jhi.com
  [1] =&gt; www.pqr.com
  [2] =&gt; www.stu.com
  )
  </code></pre>
  
  <p>i need all as above defined output.</p>
  
  <p>Please suggest the way.
  Thanks in advance.</p>