1
$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 is the input array, I need output with sort by time and unique by referral_url.

Resultant will 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 by time then remove the duplicate by referral_url.

My Code, what i tried-

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 => $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 it only result me referral_url-

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

i need all as above defined output.

Please suggest the way. Thanks in advance.

Amrish Khatri
  • 120
  • 1
  • 1
  • 7
  • no i dont think so. also I tried that code but it not works for me. in this i want unique on referral_url also. I am able to find the sort but not getting how to get full array not only referral_url with their other key value. – Amrish Khatri Jul 14 '15 at 11:04

1 Answers1

1

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 as $value){
    $hash = $value['referral_url'];
    if(!isset($result[$hash])){
       $result[$hash] = $value;
    }
}
print_r(array_values($result));

Explanation :

Used uasort to sort an array based on time which will result into 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 an array of values having unique referral_url which will result into 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 finally used array_values for values only having integer keys

Fiddle

deceze
  • 510,633
  • 85
  • 743
  • 889
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54