使用宇宙飞船运算符按两个参数对数组进行排序
usort array by two parameters using spaceship operator
有没有一种更紧凑的方法可以通过 two parameters/fields 对 PHP ≥7.0 的数组进行排序(使用 spaceship operator <=>
) ?
现在我的技巧是先按第二个参数排序,然后再按第一个参数排序:
// Sort by second parameter title
usort($products, function ($a, $b) {
return $a['title'] <=> $b['title']; // string
});
// Sort by first parameter brand_order
usort($products, function ($a, $b) {
return $a['brand_order'] <=> $b['brand_order']; // numeric
});
这给了我想要的结果;产品首先按品牌订购,然后按名称订购。
我只是想知道他们是否是一个 usort
调用的方法。
这里是我的问题代码片段。这个例子可以测试here.
<?php
<!-- Example array -->
$products = array();
$products[] = array("title" => "Title A",
"brand_name" => "Brand B",
"brand_order" => 1);
$products[] = array("title" => "Title C",
"brand_name" => "Brand A",
"brand_order" => 0);
$products[] = array("title" => "Title E",
"brand_name" => "Brand A",
"brand_order" => 0);
$products[] = array("title" => "Title D",
"brand_name" => "Brand B",
"brand_order" => 1);
// Sort by second parameter title
usort($products, function ($a, $b) {
return $a['title'] <=> $b['title']; // string
});
// Sort by first parameter brand_order
usort($products, function ($a, $b) {
return $a['brand_order'] <=> $b['brand_order']; // numeric
});
// Output
foreach( $products as $value ){
echo $value['brand_name']." — ".$value['title']."\n";
}
?>
usort($products, function ($a, $b) {
if ( $a['brand_order'] == $b["brand_order"] ) { //brand_order are same
return $a['title'] <=> $b['title']; //sort by title
}
return $a['brand_order'] <=> $b['brand_order']; //else sort by brand_order
});
有没有一种更紧凑的方法可以通过 two parameters/fields 对 PHP ≥7.0 的数组进行排序(使用 spaceship operator <=>
) ?
现在我的技巧是先按第二个参数排序,然后再按第一个参数排序:
// Sort by second parameter title
usort($products, function ($a, $b) {
return $a['title'] <=> $b['title']; // string
});
// Sort by first parameter brand_order
usort($products, function ($a, $b) {
return $a['brand_order'] <=> $b['brand_order']; // numeric
});
这给了我想要的结果;产品首先按品牌订购,然后按名称订购。
我只是想知道他们是否是一个 usort
调用的方法。
这里是我的问题代码片段。这个例子可以测试here.
<?php
<!-- Example array -->
$products = array();
$products[] = array("title" => "Title A",
"brand_name" => "Brand B",
"brand_order" => 1);
$products[] = array("title" => "Title C",
"brand_name" => "Brand A",
"brand_order" => 0);
$products[] = array("title" => "Title E",
"brand_name" => "Brand A",
"brand_order" => 0);
$products[] = array("title" => "Title D",
"brand_name" => "Brand B",
"brand_order" => 1);
// Sort by second parameter title
usort($products, function ($a, $b) {
return $a['title'] <=> $b['title']; // string
});
// Sort by first parameter brand_order
usort($products, function ($a, $b) {
return $a['brand_order'] <=> $b['brand_order']; // numeric
});
// Output
foreach( $products as $value ){
echo $value['brand_name']." — ".$value['title']."\n";
}
?>
usort($products, function ($a, $b) {
if ( $a['brand_order'] == $b["brand_order"] ) { //brand_order are same
return $a['title'] <=> $b['title']; //sort by title
}
return $a['brand_order'] <=> $b['brand_order']; //else sort by brand_order
});