Multidimensional array sort and unique by key- PHP -


this question has answer here:

$mp = array( array('url'=>'www.abc.com','time'=>'1433551154','referral_url'=>'www.pqr.com'), array('url'=>'www.xyz.com','time'=>'1433551150','referral_url'=>'www.stu.com'), array('url'=>'www.lmn.com','time'=>'1433551190','referral_url'=>'www.jhi.com'), array('url'=>'www.rst.com','time'=>'1433551170','referral_url'=>'www.pqr.com') ); 

the above input array, need output sort time , unique referral_url.

resultant be-

$mp = array( array('url'=>'www.lmn.com','time'=>'1433551190','referral_url'=>'www.jhi.com'), array('url'=>'www.rst.com','time'=>'1433551170','referral_url'=>'www.pqr.com'), array('url'=>'www.lmn.com','time'=>'1433551150','referral_url'=>'www.stu.com'), ); 

remember first sort time remove duplicate referral_url.

my code, tried-

public function unique_sort($arrs, $id) {     $unique_arr = array();     foreach ($arrs $arr) {          if (!in_array($arr[$id], $unique_arr)) {             $unique_arr[] = $arr[$id];         }     }     sort($unique_arr);     return $unique_arr; }  foreach($mp $key => $row){         $referral_url[$key]  = $row['referral_url'];         $time[$key] = $row['time'];         $url[$key] = $row['url'];     } array_multisort($time, sort_desc, $mp); $sort_arr = $this->unique_sort($mp, 'referral_url'); print_r($uniquearray);exit; 

but result me referral_url-

array( [0] => www.jhi.com [1] => www.pqr.com [2] => www.stu.com ) 

i need above defined output.

please suggest way. in advance.

try this

$mp = array( array('url'=>'www.abc.com','time'=>'1433551154','referral_url'=>'www.pqr.com'), array('url'=>'www.xyz.com','time'=>'1433551150','referral_url'=>'www.stu.com'), array('url'=>'www.lmn.com','time'=>'1433551190','referral_url'=>'www.jhi.com'), array('url'=>'www.rst.com','time'=>'1433551170','referral_url'=>'www.pqr.com') ); uasort($mp,function($a,$b){     return $b['time'] - $a['time']; });  foreach($mp $value){     $hash = $value['referral_url'];     if(!isset($result[$hash])){        $result[$hash] = $value;     } } print_r(array_values($result)); 

explanation :

used uasort sort array based on time result following array

array (     [2] => array         (             [url] => www.lmn.com             [time] => 1433551190             [referral_url] => www.jhi.com         )      [3] => array         (             [url] => www.rst.com             [time] => 1433551170             [referral_url] => www.pqr.com         )      [0] => array         (             [url] => www.abc.com             [time] => 1433551154             [referral_url] => www.pqr.com         )      [1] => array         (             [url] => www.xyz.com             [time] => 1433551150             [referral_url] => www.stu.com         )  ) 

and within foreach loop making array of values having unique referral_url result following array

array (     [www.jhi.com] => array         (             [url] => www.lmn.com             [time] => 1433551190             [referral_url] => www.jhi.com         )      [www.pqr.com] => array         (             [url] => www.rst.com             [time] => 1433551170             [referral_url] => www.pqr.com         )      [www.stu.com] => array         (             [url] => www.xyz.com             [time] => 1433551150             [referral_url] => www.stu.com         )  ) 

and used array_values values having integer keys

fiddle


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -