I have array like this and I want to unique it on basis of sender_id
, receiver_id
and classifieds_id
if these 3 columns are matched in 2 array then I want to remove old one on basis of created column if any 2 column matched nothing will happen only on matched of 3 column array will be removed
Array (
[0] => Array ( [0] => 131826 [user_id] => 131826 [1] => 131826 [sender_id] => 131826 [2] => 141332 [receiver_id] => 141332 [3] => 1055971 [classifieds_id] => 1055971 [4] => 1 [status] => 1 [5] => 2016-04-07 12:37:42 [created] => 2016-04-07 12:37:42 )
[1] => Array ( [0] => 141332 [user_id] => 141332 [1] => 141332 [sender_id] => 141332 [2] => 131826 [receiver_id] => 131826 [3] => 1055971 [classifieds_id] => 1055971 [4] => 1 [status] => 1 [5] => 2016-04-07 12:30:06 [created] => 2016-04-07 12:30:06 )
[2] => Array ( [0] => 141332 [user_id] => 141332 [1] => 141332 [sender_id] => 141332 [2] => 131826 [receiver_id] => 131826 [3] => 1055971 [classifieds_id] => 1055971 [4] => 1 [status] => 1 [5] => 2016-04-07 12:28:32 [created] => 2016-04-07 12:28:32 )
[3] => Array ( [0] => 131826 [user_id] => 131826 [1] => 131826 [sender_id] => 131826 [2] => 141332 [receiver_id] => 141332 [3] => 1055971 [classifieds_id] => 1055971 [4] => 1 [status] => 1 [5] => 2016-04-06 12:28:53 [created] => 2016-04-06 12:28:53 )
)
Here's a quick way by building composite keys that will remove duplicates. But first sort by date descending so that oldest are first and will be overwritten in the loop. If it is a SQL query and you can order it in the query then the sort is not needed:
array_multisort(array_column($array, 'created'), SORT_DESC, $array);
foreach($array as $v) {
$result[$v['sender_id'].'-'.$v['receiver_id'].'-'.$v['classifieds_id']] = $v;
}
// if you want to re-index
$result = array_values($result);
Actually, if this is a database query there may be a way to SELECT DISTINCT
but a SQL guru will need to answer that one.
Considering your Array as follows.
$classifiedArr[] = array("user_id" => "131826", "sender_id" => "131826", "receiver_id" => "141332", "classifieds_id" => "1055971", "status" => "1", "created" => "2016-04-07 12:37:42");
$classifiedArr[] = array("user_id" => "141332", "sender_id" => "141332", "receiver_id" => "131826", "classifieds_id" => "1055971", "status" => "1", "created" => "2016-04-07 12:30:06");
$classifiedArr[] = array("user_id" => "141332", "sender_id" => "141332", "receiver_id" => "131826", "classifieds_id" => "1055971", "status" => "1", "created" => "2016-04-07 12:28:32");
$classifiedArr[] = array("user_id" => "131826", "sender_id" => "131826", "receiver_id" => "141332", "classifieds_id" => "1055971", "status" => "1", "created" => "2016-04-06 12:28:53");
foreach($classifiedArr as $indClassified) {
$createUnique = $indClassified["user_id"]."|".$indClassified["sender_id"]."|".$indClassified["receiver_id"];
$newArray[$createUnique] = $indClassified;
}
logic is simple and self-explanatory, here i created a new Array, which holds unique array key with combination of user_id - sender_id - receiver_id
.