如何在多维数组中找到最大值?

时间:2010-09-25 10:20:39

标签: php arrays multidimensional-array

我有一个看起来像这样的数组:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [product] => 2003
                    [date] => 2010-09-15 13:27:35
                    [status] => 3
                )

            [1] => Array
                (
                    [product] => 2004
                    [date] => 2010-09-18 13:27:35
                    [status] => 1
                )

            [2] => Array
                (
                    [product] => 2004
                    [date] => 2010-09-18 13:27:35
                    [status] => 6
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [product] => 2003
                    [date] => 2010-09-12 13:27:35
                    [status] => 1
                )

            [1] => Array
                (
                    [product] => 2004
                    [date] => 2010-09-18 13:27:35
                    [status] => 4
                )

            [2] => Array
                (
                    [product] => 2004
                    [date] => 2010-09-18 13:27:35
                    [status] => 1
                )

        )

    [2] => Array
        (
            [0] => 

            [1] => Array
                (
                    [product] => 2004
                    [date] => 2010-09-18 13:27:35
                    [status] => 1
                )

            [2] => Array
                (
                    [product] => 2004
                    [date] => 2010-09-18 13:27:35
                    [status] => 1
                )

        )

我想“折叠”每个第二维数组并获取最大DATE值和最大状态值。因此第一个索引将返回2010-09-18 13:27:35和'6'等。

最后一个索引中的空数组使问题更加复杂。我想使用这个空数组并将其报告为MAX日期和状态。

提前感谢!

1 个答案:

答案 0 :(得分:1)

感谢您寻找所有人。我想通了。

$date=array();
$status=array();
$availability=array();
    foreach($set as $key => $value)
    {
        foreach($value as $value2)
        {

            if(isset($value2[1]))
            {
            $date[$key][]=$value2[1];
            $status[$key][]=$value2[2];

            }
            else
            {
            $date[$key][]='2022-09-18 13:27:35';
            $status[$key][]='0';
            }

        }


                $availability[$key]=array(max($date[$key]),min($status[$key]));

     }