我有数组这样的,我想在的基础上独一无二的它sender_id
,receiver_id
而classifieds_id
如果这3列2阵列匹配,然后我想删除上创建列的基础上旧的,如果只对匹配的3任2列匹配什么都不会发生列数组将被删除
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 )
)
这是通过构建组合键来删除重复项的快速方法。但是,第一个按日期降序排列,以便最老的优先,并且在循环中将被覆盖。如果这是一个SQL查询,您可以在查询中对其进行排序,则不需要排序:
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);
实际上,如果这是数据库查询,则可能有一种方法,SELECT DISTINCT
但是SQL专家将需要回答该问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句