简体   繁体   English

排序多维数组(PHP) - 日期并发症和计数

[英]Sort multidimensional array (PHP) - date complications and counting

I have the following output of an array using PHP. 我有使用PHP的数组的以下输出。 I need to do two things... First, I need to sort the array so it prints by the most recent date. 我需要做两件事......首先,我需要对数组进行排序,以便在最近的日期打印。 I can't use a simple sort, because the date is outputted in the format mm/dd/yyyy (and not a regular time stamp) ... 我不能使用简单的排序,因为日期以mm / dd / yyyy格式输出(而不是常规时间戳)...

Then I need to count how many rows exist for each year. 然后我需要计算每年存在多少行。 So, in the example below, I would need to know that there are ... 所以,在下面的例子中,我需要知道有......

  • 2 entries from 2010 2010年的2个参赛作品
  • 2 entries from 2011 2011年2项
  • 1 entry from 2012 2012年1次入境
  • Stop counting when there are no more rows 没有更多行时停止计数

Since the year is not separate from the rest of the date digits, this also complicates things... 由于年份与其他日期数字没有分开,这也使事情复杂化......

Array
(
    [0] => Array
        (
            [racer_date] => 11/15/2010
            [racer_race] => Test Row 4
            [racer_event] => 321
            [racer_time] => 16
            [racer_place] => 12
            [racer_medal] => 1
        )

    [1] => Array
        (
            [racer_date] => 7/15/2010
            [racer_race] => Test Row 3
            [racer_event] => 123
            [racer_time] => 14
            [racer_place] => 6
            [racer_medal] => 0
        )

    [2] => Array
        (
            [racer_date] => 7/28/2011
            [racer_race] => Test Row
            [racer_event] => 123
            [racer_time] => 10
            [racer_place] => 2
            [racer_medal] => 2
        )

    [3] => Array
        (
            [racer_date] => 10/9/2011
            [racer_race] => Test Row 2
            [racer_event] => 321
            [racer_time] => 12
            [racer_place] => 3
            [racer_medal] => 3
        )

    [4] => Array
        (
            [racer_date] => 10/3/2012
            [racer_race] => World Indoor Championships (final)
            [racer_event] => 400m
            [racer_time] => 50.79
            [racer_place] => 1
            [racer_medal] => 1
        )

)
function cmp($a, $b)
{
    if (strtotime($a["racer_date"]) == strtotime($b["racer_date"])) {
        return 0;
    }
    return (strtotime($a["racer_date"]) < strtotime($b["racer_date"])) ? -1 : 1;
}

usort($array, "cmp");

call your array $array, and above code will sort it.. And to count entities you'll need to run foreach and check date('Y',strtotime($a["racer_date"])) in that foreach which will give you year in 4 digit.. 调用你的数组$ array,上面的代码将对它进行排序..并计算实体你需要运行foreach并检查date('Y',strtotime($a["racer_date"]))在foreach中将给出你今年4位数..

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

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