从数组中查找最大月份和相似月份并生成新数组

时间:2018-06-20 07:37:50

标签: php arrays multidimensional-array comparison

我有下面的类型数组输出,我想从n个以下类型数组中获得最大月份,而且月份和年份必须在所有n个数组中都可用。

Array
(
 [0] => Array
        (
            [0] => Array
                (
                    [month] => 08
                    [year] => 2003
                    [month-year] => 08-2003
                    [strtotime] => 1059676200
                    [percentage] => 1.97
                )

            [1] => Array
                (
                    [month] => 09
                    [year] => 2003
                    [month-year] => 09-2003
                    [strtotime] => 1062354600
                    [percentage] => 5.09
                )

            [2] => Array
                (
                    [month] => 10
                    [year] => 2003
                    [month-year] => 10-2003
                    [strtotime] => 1064946600
                    [percentage] => -0.01
                )

            [3] => Array
                (
                    [month] => 11
                    [year] => 2003
                    [month-year] => 11-2003
                    [strtotime] => 1067625000
                    [percentage] => 0.75
                )

            [4] => Array
                (
                    [month] => 12
                    [year] => 2003
                    [month-year] => 12-2003
                    [strtotime] => 1070217000
                    [percentage] => 5.22
                )

            [5] => Array
                (
                    [month] => 01
                    [year] => 2004
                    [month-year] => 01-2004
                    [strtotime] => 1072895400
                    [percentage] => 1.5
                )

            [6] => Array
                (
                    [month] => 02
                    [year] => 2004
                    [month-year] => 02-2004
                    [strtotime] => 1075573800
                    [percentage] => 1.8
                )

            [7] => Array
                (
                    [month] => 03
                    [year] => 2004
                    [month-year] => 03-2004
                    [strtotime] => 1078079400
                    [percentage] => 0.79
                )

            [8] => Array
                (
                    [month] => 04
                    [year] => 2004
                    [month-year] => 04-2004
                    [strtotime] => 1080757800
                    [percentage] => -0.52
                )

            [9] => Array
                (
                    [month] => 05
                    [year] => 2004
                    [month-year] => 05-2004
                    [strtotime] => 1083349800
                    [percentage] => -2.19
                )

            [10] => Array
                (
                    [month] => 06
                    [year] => 2004
                    [month-year] => 06-2004
                    [strtotime] => 1086028200
                    [percentage] => 1.36
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [month] => 11
                    [year] => 2002
                    [month-year] => 11-2002
                    [strtotime] => 1036089000
                    [percentage] => 0.05
                )

            [1] => Array
                (
                    [month] => 12
                    [year] => 2002
                    [month-year] => 12-2002
                    [strtotime] => 1038681000
                    [percentage] => -0.1
                )

            [2] => Array
                (
                    [month] => 01
                    [year] => 2003
                    [month-year] => 01-2003
                    [strtotime] => 1041359400
                    [percentage] => -0.31
                )

            [3] => Array
                (
                    [month] => 02
                    [year] => 2003
                    [month-year] => 02-2003
                    [strtotime] => 1044037800
                    [percentage] => -0.88
                )

            [4] => Array
                (
                    [month] => 03
                    [year] => 2003
                    [month-year] => 03-2003
                    [strtotime] => 1046457000
                    [percentage] => 1
                )

            [5] => Array
                (
                    [month] => 04
                    [year] => 2003
                    [month-year] => 04-2003
                    [strtotime] => 1049135400
                    [percentage] => 1.63
                )

            [6] => Array
                (
                    [month] => 05
                    [year] => 2003
                    [month-year] => 05-2003
                    [strtotime] => 1051727400
                    [percentage] => 1.35
                )

            [7] => Array
                (
                    [month] => 06
                    [year] => 2003
                    [month-year] => 06-2003
                    [strtotime] => 1054405800
                    [percentage] => 0.05
                )

            [8] => Array
                (
                    [month] => 07
                    [year] => 2003
                    [month-year] => 07-2003
                    [strtotime] => 1056997800
                    [percentage] => 0.5
                )

            [9] => Array
                (
                    [month] => 08
                    [year] => 2003
                    [month-year] => 08-2003
                    [strtotime] => 1059676200
                    [percentage] => 0.65
                )

        )

)

这里是输出,因此任何人都可以得到与我相似的具有最大月份的月份和年份数组。

我通过使用下面的foreach循环从数组中找到了最大月份。

$max = -9999999; 
    $found_item = null; 

    $h = 0;

    foreach($master_array as $k => $v)
    {
        if($v[0]['strtotime'] > $max)
        {
           $max = $v[0]['strtotime'];
           $found_item = $v;
           break;
        }
    }

此处主数组是保存上面数组详细信息的变量。因此,可以使用最大数组,但是我无法比较同一变量的所有月份和年份。

所以任何人都知道我该怎么做。

0 个答案:

没有答案