3 回答
TA贡献1863条经验 获得超2个赞
您的问题基本上是如何根据两个键对数组进行排序。在许多编程语言中,包括PHP,答案是创建一个比较函数(有时称为比较器),该函数根据其参数所需的相对顺序返回值(通常为-1,0,1)。如果两个键中的第一个参数不同,则根据第一个键的顺序返回 -1 或 1。它们在第一个键上相等,检查第二个键 - 并根据第二个键的顺序返回-1,0,1。此逻辑自然可以扩展到任意数量的密钥。PHP Spaceship 运算符 <=>
为许多内置类型实现了此逻辑。
下面的代码演示了使用 PHP 的方案的这一点。它是从这个问题改编而来的。在这里,我假设您要按速率(零<超减<减<中等<标准)进行排序,然后按(数字)进行排序。我也纠正了.spec_id
SUPER REDUCES RATE
SUPER REDUCED RATE
$RATE_ORDERING = array(
'ZERO RATE' => 1,
'SUPER REDUCED RATE' => 2,
'REDUCED RATE' => 3,
'MEDIUM RATE' => 4,
'STANDARD RATE' => 5
);
$arr = array(
array('spec_id' => 2, 'spec_children_name' => 'STANDARD RATE'),
array('spec_id' => 1, 'spec_children_name' => 'STANDARD RATE'),
array('spec_id' => 2, 'spec_children_name' => 'ZERO RATE'),
array('spec_id' => 1, 'spec_children_name' => 'MEDIUM RATE'),
array('spec_id' => 2, 'spec_children_name' => 'REDUCED RATE'),
array('spec_id' => 2, 'spec_children_name' => 'MEDIUM RATE'),
array('spec_id' => 1, 'spec_children_name' => 'SUPER REDUCED RATE'),
array('spec_id' => 2, 'spec_children_name' => 'SUPER REDUCED RATE'),
array('spec_id' => 1, 'spec_children_name' => 'REDUCED RATE'),
array('spec_id' => 1, 'spec_children_name' => 'ZERO RATE')
);
usort($arr, function ($a, $b) use ($RATE_ORDERING) {
$result = $RATE_ORDERING[$a['spec_children_name']] - $RATE_ORDERING[$b['spec_children_name']];
if ($result != 0) {
return $result;
}
return $a['spec_id'] - $b['spec_id'];
});
var_dump($arr);
输出:
array(10) {
[0]=>
array(2) {
["spec_id"]=>
int(1)
["spec_children_name"]=>
string(9) "ZERO RATE"
}
[1]=>
array(2) {
["spec_id"]=>
int(2)
["spec_children_name"]=>
string(9) "ZERO RATE"
}
[2]=>
array(2) {
["spec_id"]=>
int(1)
["spec_children_name"]=>
string(18) "SUPER REDUCED RATE"
}
[3]=>
array(2) {
["spec_id"]=>
int(2)
["spec_children_name"]=> [17/133]
string(18) "SUPER REDUCED RATE"
}
[4]=>
array(2) {
["spec_id"]=>
int(1)
["spec_children_name"]=>
string(12) "REDUCED RATE"
}
[5]=>
array(2) {
["spec_id"]=>
int(2)
["spec_children_name"]=>
string(12) "REDUCED RATE"
}
[6]=>
array(2) {
["spec_id"]=>
int(1)
["spec_children_name"]=>
string(11) "MEDIUM RATE"
}
[7]=>
array(2) {
["spec_id"]=>
int(2)
["spec_children_name"]=>
string(11) "MEDIUM RATE"
}
[8]=>
array(2) {
["spec_id"]=>
int(1)
["spec_children_name"]=>
string(13) "STANDARD RATE"
}
[9]=>
array(2) {
["spec_id"]=>
int(2)
["spec_children_name"]=>
string(13) "STANDARD RATE"
}
}
TA贡献1995条经验 获得超2个赞
宇宙飞船操作员将自动处理所有数据类型。将动态列名和排序方向“factor”传递到自定义函数作用域中。use()
代码:
$objects = json_decode($json);
$column = 'spec_children_name';
$direction = 'asc';
$reverser = $direction === 'asc' ? 1 : -1;
uasort($objects, function($a, $b) use ($column, $reverser) {
return $reverser * ($a->$column <=> $b->$column);
});
var_export($objects);
或者也许...
$objects = json_decode($json);
$rules = ['spec_id' => 'ASC', 'spec_children_name' => 'DESC'];
uasort($objects, function($a, $b) use ($rules) {
foreach ($rules as $column => $order) {
$left[] = $order === 'ASC' ? $a->$column : $b->$column;
$right[] = $order === 'ASC' ? $b->$column : $a->$column;
}
return $left <=> $right;
});
var_export($objects);
TA贡献1884条经验 获得超4个赞
几乎没有理由像array_multisort和array_column那样重新发明一个功能。
$data =
[
[
'name' => 'John',
'age' => 34
],
[
'name' => 'Jack',
'age' => 55,
],
[
'name' => 'Adam',
'age' => 42
],
[
'name' => 'Jack',
'age' => 78
],
[
'name' => 'Adam',
'age' => 80
]
];
array_multisort(array_column($data, 'name'), SORT_ASC, array_column($data, 'age'), SORT_DESC, $data);
var_export($data);
输出:
array (
0 =>
array (
'name' => 'Adam',
'age' => 80,
),
1 =>
array (
'name' => 'Adam',
'age' => 42,
),
2 =>
array (
'name' => 'Jack',
'age' => 78,
),
3 =>
array (
'name' => 'Jack',
'age' => 55,
),
4 =>
array (
'name' => 'John',
'age' => 34,
),
)
- 3 回答
- 0 关注
- 112 浏览
添加回答
举报