Php 使用两个条件对数组进行自定义排序
我有以下数组:Php 使用两个条件对数组进行自定义排序,php,sorting,multidimensional-array,Php,Sorting,Multidimensional Array,我有以下数组: $arr = [ [ 'user_id' => 1, 'product_id' => 1 ], [ 'user_id' => 1, 'product_id' => 2 ], [ 'user_id' => 1, 'product_id' => 3 ], [ 'user_id' =&
$arr = [
[
'user_id' => 1,
'product_id' => 1
],
[
'user_id' => 1,
'product_id' => 2
],
[
'user_id' => 1,
'product_id' => 3
],
[
'user_id' => 2,
'product_id' => 1
],
[
'user_id' => 2,
'product_id' => 2
],
[
'user_id' => 3,
'product_id' => 1
]
];
我想对它进行排序,使其看起来像这样:
$arr = [
[
'user_id' => 1,
'product_id' => 1
],
[
'user_id' => 2,
'product_id' => 1
],
[
'user_id' => 3,
'product_id' => 1
],
[
'user_id' => 1,
'product_id' => 2
],
[
'user_id' => 2,
'product_id' => 2
],
[
'user_id' => 1,
'product_id' => 3
]
];
因此,基本上我需要按product\u id
和user\u id
订购,以便在继续下一步之前从每个用户处选择较低的编号product\u id
我试着使用usort,但无法使其工作
usort($campaigns, function($a, $b){
if($a['product_id'] == $b['product_id']){
return 0;
}
if($a['product_id'] < $b['product_id']){
if($a['user_id'] == $b['user_id']){
return 1;
}
if($a['user_id'] < $a['user_id']){
return 0;
}
return -1;
}else{
if($a['user_id'] == $a['user_id']){
return -1;
}
if($a['user_id'] < $a['user_id']){
return 0;
}
return 1;
}
});
usort($campetings,function($a,$b){
如果($a['product\U id']=$b['product\U id'])){
返回0;
}
如果($a['product\U id']<$b['product\U id'])){
如果($a['user\u id']=$b['user\u id'])){
返回1;
}
如果($a['user\u id']<$a['user\u id']){
返回0;
}
返回-1;
}否则{
如果($a['user\u id']=$a['user\u id']){
返回-1;
}
如果($a['user\u id']<$a['user\u id']){
返回0;
}
返回1;
}
});
我还尝试了
array\u multisort
,但我所能做的就是使用我已经从数据库中检索到的相同顺序进行排序。假设您的值是整数:
usort($campaigns, function($a, $b){
if($a['product_id'] == $b['product_id']){
return $a['user_id'] - $b['user_id'];
} else {
return $a['product_id'] - $b['product_id'];
}
});
此外,您还可以使用带有
orderbyproduct\u id,user\u id
子句的数据库排序
$arrTags = [
[
'user_id' => 1,
'product_id' => 1
],
[
'user_id' => 1,
'product_id' => 2
],
[
'user_id' => 1,
'product_id' => 3
],
[
'user_id' => 2,
'product_id' => 1
],
[
'user_id' => 2,
'product_id' => 2
],
[
'user_id' => 3,
'product_id' => 1
]
];
foreach($arrTags as $key => $row){
$userArray[$key] = $row['user_id'];
$productArray[$key] = $row['product_id'];
}
array_multisort($productArray, SORT_ASC, $userArray, SORT_ASC, $arrTags);
print_r($arrTags);
输出
Array
(
[0] => Array
(
[user_id] => 1
[product_id] => 1
)
[1] => Array
(
[user_id] => 2
[product_id] => 1
)
[2] => Array
(
[user_id] => 3
[product_id] => 1
)
[3] => Array
(
[user_id] => 1
[product_id] => 2
)
[4] => Array
(
[user_id] => 2
[product_id] => 2
)
[5] => Array
(
[user_id] => 1
[product_id] => 3
)
)
您还可以签入在线编辑器使用带有“列数组”(很少的排序维度)的
array\u multisort
函数的解决方案:
输出:
Array
(
[0] => Array
(
[user_id] => 1
[product_id] => 1
)
[1] => Array
(
[user_id] => 2
[product_id] => 1
)
[2] => Array
(
[user_id] => 3
[product_id] => 1
)
[3] => Array
(
[user_id] => 1
[product_id] => 2
)
[4] => Array
(
[user_id] => 2
[product_id] => 2
)
[5] => Array
(
[user_id] => 1
[product_id] => 3
)
)
您使用
usort
的代码不正确;您应该按第一个字段与
进行比较,并且仅当第一个字段等于按第二个字段进行比较时才进行比较。您是说您正在从数据库中检索,您没有直接对查询进行排序的原因是什么?您的输出与OP预期的输出不一样。此外,你关于检查答案的评论完全是噪音和无用的。虽然这段代码片段可以解决这个问题,但确实有助于提高你文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。还请尽量不要用解释性注释挤满你的代码,这会降低代码和解释的可读性!请检查我编辑的代码。我只是忘了在multisort中更改$userArray和$productArray。我已经编辑了代码,并在下面的链接编辑器中签入了输出。我并不像你们那个样聪明和专业,但我尽我所能给用户提供有用的答案@Rizier123很抱歉给您带来不便,因为我已附加输出,但未检查其“我的错误”。数组\u multisort($productArray,SORT\u ASC,$userArray,SORT\u ASC,$arrTags)中的数组交换错误@里泽尔123
Array
(
[0] => Array
(
[user_id] => 1
[product_id] => 1
)
[1] => Array
(
[user_id] => 2
[product_id] => 1
)
[2] => Array
(
[user_id] => 3
[product_id] => 1
)
[3] => Array
(
[user_id] => 1
[product_id] => 2
)
[4] => Array
(
[user_id] => 2
[product_id] => 2
)
[5] => Array
(
[user_id] => 1
[product_id] => 3
)
)