简体   繁体   English

PHP使用array_multisort按日期对多个数组进行排序

[英]PHP using array_multisort to sort multiple arrays by date

I have around five arrays, where one of them is containing dates. 我大约有五个数组,其中一个包含日期。 I would like to sort these after the array containing dates. 我想在包含日期的数组之后对它们进行排序。 I know how to sort the date array using "usort". 我知道如何使用“ usort”对日期数组进行排序。 I also know how to sort multiple arrays using "array_multisort", but do not know how to combine these. 我也知道如何使用“ array_multisort”对多个数组进行排序,但是不知道如何将它们组合在一起。 Is there a way doing this? 有办法吗? I hope you can help, and tell me if you need more information to solve my problem :) 希望您能提供帮助,并告诉我是否需要更多信息来解决我的问题:)

Edit: 编辑:

Here are my arrays: 这是我的数组:

$fid=array(1, 2, 3, 4, 5, 6, 7, 8);
$ftitle=array("Title1", "Title2", "Title3", "Title4", "Title1", "Title2", "Title3", "Title4");
$fbeskrivelse=array("Beskrivelse1", "Beskrivelse2", "Beskrivelse3", "Beskrivelse4", "Beskrivelse1", "Beskrivelse2", "Beskrivelse3", "Beskrivelse4");
$fstøtter=array(2, 15, 7, 10, 3, 4, 5, 6);
$fstartdato=array('11-01-2012', '01-01-2014', '01-01-2015', '09-02-2013', '01-01-2013', '11-01-2017', '01-01-2018', '01-01-2019');

So this is the kind of result I want (after the sorting): 所以这是我想要的结果(排序后):

$fid=array(1, 3, 4, 2, 5, 6, 7, 8);
$ftitle=array("Title1", "Title3", "Title4", "Title2", "Title1", "Title2", "Title3", "Title4");
$fbeskrivelse=array("Beskrivelse1", "Beskrivelse3", "Beskrivelse4", "Beskrivelse2", "Beskrivelse1", "Beskrivelse2", "Beskrivelse3", "Beskrivelse4");
$fstøtter=array(2, 7, 10, 15, 3, 4, 5, 6);
$fstartdato=array('11-01-2012', '01-01-2013', '09-02-2013', '01-01-2014', '01-01-2015', '11-01-2017', '01-01-2018', '01-01-2019');

If I'm understanding correctly, you have (lets say 3) arrays that you want to sort, one of which contains dates, and you want to sort by the dates, and since multisort does not support a callback or sort on dates, you aren't sure what to do? 如果我的理解正确,您有(要说3个)要排序的数组,其中一个包含日期,并且要按日期排序,并且由于multisort不支持回调或按日期排序,因此您可以不确定该怎么办?

eg 例如

$arr1 = array('2019-05-15', '2019-05-17', '2019-05-13')
$arr2 = array('Wed','Fri','Mon');
$arr3 = array('Pork','Steak','Chicken');

If it were me, I'd probably just parse the dates into unix_time in a new array and use that new array as my sorting "key" to sort the rest, since multisort can do numbers. 如果是我,我可能只是将日期解析到一个新数组中的unix_time中,并使用该新数组作为我的排序“键”来对其余部分进行排序,因为多重排序可以计算数字。

(Not tested, just my theory) (未经测试,仅根据我的理论)

$key = array_map('strtotime', $arr1);
array_multisort($key, SORT_ASC, SORT_NUMERIC, $arr1, $arr2, $arr3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM