我有以下数组:
Array
(
[Sir Ruane] => Array
(
[2018-07-03] => Array
(
[Product 1] => Array
(
[Paid] => Array
(
[Ticket] => Array
(
[Standard] => Array
(
[quantity] => 6
[asistance] => 6
)
)
)
)
)
[2018-07-01] => Array
(
[Product 1] => Array
(
[Paid] => Array
(
[Ticket] => Array
(
[Standard] => Array
(
[quantity] => 2
[asistance] => 2
)
)
)
)
)
[2018-07-06] => Array
(
[Product 1] => Array
(
[Paid] => Array
(
[Ticket] => Array
(
[Standard] => Array
(
[quantity] => 5
[asistance] => 5
)
)
)
)
)
)
)
我正在尝试按日期格式升序按键以具有日期格式的键对数组进行排序。
赞:
客户名称->日期(降序排列)->产品->状态->等。
我尝试了以下内容,因为我已经在Stackoverflow社区的另一篇文章中阅读了它:
array_multisort( array_column($array, $array[0][0]), SORT_ASC, $array );
但这给了我第一个编写的数组。
我还尝试了其他功能,例如ksort(),sort(),usort(),但是我要实现的目标没有任何作用。
我还阅读了sort()函数可以具有用于对事物进行排序的自定义函数,但是我已阅读的所有示例均基于列名,而我没有列名,因为键始终根据用户名而变化,日期,产品名称,状态,票证类型和票证名称。
有人可以帮我吗?
答案 0 :(得分:0)
这可能是您想要的:
$array=
Array
(
"Sir Ruane" => Array
(
"2018-07-03" => Array
(
"Product 1" => Array
(
"Paid" => Array
(
"Ticket" => Array
(
"Standard" => Array
(
"quantity" => 6,
"asistance" => 6
)
)
)
)
),
"2018-07-01" => Array
(
"Product 1" => Array
(
"Paid" => Array
(
"Ticket" => Array
(
"Standard" => Array
(
"quantity" => 2,
"asistance" => 2
)
)
)
)
),
"2018-07-06" => Array
(
"Product 1" => Array
(
"Paid" => Array
(
"Ticket" => Array
(
"Standard" => Array
(
"quantity" => 5,
"asistance" => 5
)
)
)
)
)
)
);
reset($array);
list($k,$v)=each($array);
uksort($array[$k],function ($a,$b){
list($a_year,$a_month,$a_day)=explode('-',$a);
list($b_year,$b_month,$b_day)=explode('-',$b);
if($a_year>$b_year) return -1;
if($a_year<$b_year) return 1;
if($a_month>$b_month) return -1;
if($a_month<$b_month) return 1;
if($a_day>$b_day) return -1;
if($a_day<$b_day) return 1;
return 0;
});
print_r($array);
,输出为:
Array
(
[Sir Ruane] => Array
(
[2018-07-06] => Array
(
[Product 1] => Array
(
[Paid] => Array
(
[Ticket] => Array
(
[Standard] => Array
(
[quantity] => 5
[asistance] => 5
)
)
)
)
)
[2018-07-03] => Array
(
[Product 1] => Array
(
[Paid] => Array
(
[Ticket] => Array
(
[Standard] => Array
(
[quantity] => 6
[asistance] => 6
)
)
)
)
)
[2018-07-01] => Array
(
[Product 1] => Array
(
[Paid] => Array
(
[Ticket] => Array
(
[Standard] => Array
(
[quantity] => 2
[asistance] => 2
)
)
)
)
)
)
)
当然,如果您有更多客户,则可以通过以下方式将代码循环放置:
reset($array);
while(list($k,$v)=each($array)){
uksort($array[$k],function ($a,$b){
list($a_year,$a_month,$a_day)=explode('-',$a);
list($b_year,$b_month,$b_day)=explode('-',$b);
if($a_year>$b_year) return -1;
if($a_year<$b_year) return 1;
if($a_month>$b_month) return -1;
if($a_month<$b_month) return 1;
if($a_day>$b_day) return -1;
if($a_day<$b_day) return 1;
return 0;
});
}