我想用'fee_category'的值对下面的数组进行排序。数组的第一个关键字是日期,在每个日期内,它可能包含多个费用,如第4个和第5个日期(即[2020-10-15]和[2020-10-14](。我只需要一个简单的方法来完成这个排序。我试过usort((,但没有按预期工作。
Array(
[2020-10-19] =>
Array(
[0] => Array(
[fee_fare] => 2650
[fee_category] => Tuition Fee
)
)
[2020-10-17] =>
Array(
[0] => Array(
[fee_fare] => 630
[fee_category] => Special Fee
)
)
[2020-10-16] =>
Array(
[0] => Array(
[fee_fare] => 800
[fee_category] => Special Fee
)
)
[2020-10-15] =>
Array(
[0] => Array(
[fee_fare] => 550
[fee_category] => Special Fee
)
[1] => Array(
[fee_fare] => 550
[fee_category] => Special Fee
)
[2] => Array(
[fee_fare] => 2650
[fee_category] => Tuition Fee
)
[3] => Array(
[fee_fare] => 3850
[fee_category] => Tuition Fee
)
[4] => Array(
[fee_fare] => 630
[fee_category] => Special Fee
)
)
[2020-10-14] =>
Array(
[0] => Array(
[fee_fare] => 550
[fee_category] => Special Fee
)
[1] => Array(
[fee_fare] => 2650
[fee_category] => Tuition Fee
)
[2] => Array(
[fee_fare] => 550
[fee_category] => Special Fee
)
[3] => Array(
[fee_fare] => 1325
[fee_category] => Tuition Fee
)
[4] => Array(
[fee_fare] => 550
[fee_category] => Special Fee
)
[5] => Array([fee_fare] => 800
[fee_category] => Special Fee
)
[6] => Array([fee_fare] => 2700
[fee_category] => Tuition Fee
)
)
)
预期输出
Array(
[2020-10-19] =>
Array(
[0] => Array(
[fee_fare] => 2650
[fee_category] => Tuition Fee
)
)
[2020-10-17] =>
Array(
[0] => Array(
[fee_fare] => 630
[fee_category] => Special Fee
)
)
[2020-10-16] =>
Array(
[0] => Array(
[fee_fare] => 800
[fee_category] => Special Fee
)
)
[2020-10-15] =>
Array(
[0] => Array(
[fee_fare] => 550
[fee_category] => Special Fee
)
[1] => Array(
[fee_fare] => 550
[fee_category] => Special Fee
)
[4] => Array(
[fee_fare] => 630
[fee_category] => Special Fee
)
[2] => Array(
[fee_fare] => 2650
[fee_category] => Tuition Fee
)
[3] => Array(
[fee_fare] => 3850
[fee_category] => Tuition Fee
)
)
[2020-10-14] =>
Array(
[0] => Array(
[fee_fare] => 550
[fee_category] => Special Fee
)
[2] => Array(
[fee_fare] => 550
[fee_category] => Special Fee
)
[4] => Array(
[fee_fare] => 550
[fee_category] => Special Fee
)
[5] => Array(
[fee_fare] => 800
[fee_category] => Special Fee
)
[1] => Array(
[fee_fare] => 2650
[fee_category] => Tuition Fee
)
[3] => Array(
[fee_fare] => 1325
[fee_category] => Tuition Fee
)
[6] => Array([fee_fare] => 2700
[fee_category] => Tuition Fee
)
)
)
提前谢谢。
以下是您需要的array_multiport与array_column 相结合的内容
$arr = [
'2020-10-19' =>
[
0 => [
'fee_fare' => 2650,
'fee_category' => 'Tuition Fee'
]
],
'2020-10-17' =>
[
0 => [
'fee_fare' => 630,
'fee_category' => 'Special Fee'
]
],
'2020-10-16' =>
[
0 => [
'fee_fare' => 800,
'fee_category' => 'Special Fee'
]
],
'2020-10-15' =>
[
0 => [
'fee_fare' => 550,
'fee_category' => 'Special Fee'
],
1 => [
'fee_fare' => 550,
'fee_category' => 'Special Fee'
],
2 => [
'fee_fare' => 2650,
'fee_category' => 'Tuition Fee'
],
3 => [
'fee_fare' => 3850,
'fee_category' => 'Tuition Fee'
],
4 => [
'fee_fare' => 630,
'fee_category' => 'Special Fee'
],
],
'2020-10-14' => [
0 => [
'fee_fare' => 550,
'fee_category' => 'Special Fee'
],
1 => [
'fee_fare' => 2650,
'fee_category' => 'Tuition Fee'
],
2 => [
'fee_fare' => 550,
'fee_category' => 'Special Fee'
],
3 => [
'fee_fare' => 1325,
'fee_category' => 'Tuition Fee'
],
4 => [
'fee_fare' => 550,
'fee_category' => 'Special Fee'
],
5 => [
'fee_fare' => 800,
'fee_category' => 'Special Fee'
],
6 => [
'fee_fare' => 2700,
'fee_category' => 'Tuition Fee'
]
]
];
foreach($arr as $key => $a){
array_multisort(
array_column($a, 'fee_fare'),
SORT_ASC,
$a
);
$arr[$key] = $a;
}
echo "<pre>";
echo "<b>".__FILE__."</b><br/>";
var_dump($arr);
echo "</pre>";
die();
Otput是:
array (size=5)
'2020-10-19' =>
array (size=1)
0 =>
array (size=2)
'fee_fare' => int 2650
'fee_category' => string 'Tuition Fee' (length=11)
'2020-10-17' =>
array (size=1)
0 =>
array (size=2)
'fee_fare' => int 630
'fee_category' => string 'Special Fee' (length=11)
'2020-10-16' =>
array (size=1)
0 =>
array (size=2)
'fee_fare' => int 800
'fee_category' => string 'Special Fee' (length=11)
'2020-10-15' =>
array (size=5)
0 =>
array (size=2)
'fee_fare' => int 550
'fee_category' => string 'Special Fee' (length=11)
1 =>
array (size=2)
'fee_fare' => int 550
'fee_category' => string 'Special Fee' (length=11)
2 =>
array (size=2)
'fee_fare' => int 630
'fee_category' => string 'Special Fee' (length=11)
3 =>
array (size=2)
'fee_fare' => int 2650
'fee_category' => string 'Tuition Fee' (length=11)
4 =>
array (size=2)
'fee_fare' => int 3850
'fee_category' => string 'Tuition Fee' (length=11)
'2020-10-14' =>
array (size=7)
0 =>
array (size=2)
'fee_fare' => int 550
'fee_category' => string 'Special Fee' (length=11)
1 =>
array (size=2)
'fee_fare' => int 550
'fee_category' => string 'Special Fee' (length=11)
2 =>
array (size=2)
'fee_fare' => int 550
'fee_category' => string 'Special Fee' (length=11)
3 =>
array (size=2)
'fee_fare' => int 800
'fee_category' => string 'Special Fee' (length=11)
4 =>
array (size=2)
'fee_fare' => int 1325
'fee_category' => string 'Tuition Fee' (length=11)
5 =>
array (size=2)
'fee_fare' => int 2650
'fee_category' => string 'Tuition Fee' (length=11)
6 =>
array (size=2)
'fee_fare' => int 2700
'fee_category' => string 'Tuition Fee' (length=11)
正如你所看到的,我们循环通过";主";数组,将其拆分为$key=>值,然后我们通过"fee_fare"列的值在子数组上使用array_multiport