如何找出最大/最新的多维数组

时间:2012-04-03 16:13:02

标签: php multidimensional-array max min

我有这些数组,[270] [269] [268]是专辑ID,[94] [86] [96] [101] ...是照片ID。如何找出最大值意味着每张专辑中的最新照片。 我的目的是循环所有照片,找出最大照片,并在每张专辑中比其他照片更大。顺便说一句,我将相册和照片值设置为此阵列中的关键。这是好事还是坏事? 谢谢你

so the result is:
Album 1 = $arr[270][97]
Album 2 = $arr[269][102]
Album 3 = $arr[268][86]

Array
(
[270] => Array
    (
        [user_email] => aaa@hotmail.com
        [story_name] => Story C
        [89] => Array
            (
                [pid] => 89
                [photo_url] => story_photos/2012/0322/361332393792911587.jpg
            )

        [90] => Array
            (
                [pid] => 90
                [photo_url] => story_photos/2012/0322/361332393792911587.jpg
            )

        [97] => Array
            (
                [pid] => 92
                [photo_url] => story_photos/2012/0402/361333367180912960.jpg
            )
      )
[269] => Array
    (
        [user_email] => aaa@hotmail.com
        [story_name] => Story B
        [87] => Array
            (
                [pid] => 87
                [photo_url] => story_photos/2012/0322/361332381406580761.jpg
            )

        [98] => Array
            (
                [pid] => 98
                [photo_url] => story_photos/2012/0402/361333369100507620.jpg
            )

        [102] => Array
            (
                [pid] => 99
                [photo_url] => story_photos/2012/0402/361333369110530609.jpg
            )
[268] => Array
    (
        [user_email] => aaa@hotmail.com
        [story_name] => Story A
        [86] => Array
            (
                [pid] => 86
                [photo_url] => story_photos/2012/0322/361332381393552719.jpg
            )

    )

)

2 个答案:

答案 0 :(得分:0)

我建议你使用类似下面的数组结构:

Array (
[270] => Array (
        [user_email] => aaa@hotmail.com
        [story_name] => Story C
        [photos] => Array(
            [89] => Array
                (
                    [pid] => 89
                    [photo_url] => story_photos/2012/0322/361332393792911587.jpg
                )

            [90] => Array
                (
                    [pid] => 90
                    [photo_url] => story_photos/2012/0322/361332393792911587.jpg
                )
        )
.
.
.
[269] => Array (...
)

并使用krsort()获取最新照片。像这样:

foreach($array as $album) {
    krsort($album['photos']);
    for($i=0; $i<count($album['photos']); $i++) {
        if ($i == 0) {
            // LATEST
        }
    }
}

答案 1 :(得分:0)

您可以使用php.net(http://php.net/manual/pt_BR/function.min.php

中提供的功能之一
    function min_by_key($arr, $key)
    {
        $min = array();
        foreach ($arr as $val) {
            if (!isset($val[$key]) and is_array($val)) 
            {
                $min2 = min_by_key($val, $key);
                $min[$min2] = 1;
            } 
            elseif (!isset($val[$key]) and !is_array($val)) 
            {
                return false;
            } 
            elseif (isset($val[$key])) 
            {
                $min[$val[$key]] = 1;
            }
        }
        return min( array_keys($min) );
    }

    function max_by_key($arr, $key)
    {
        $max = array();
        foreach ($arr as $val) {
            if (!isset($val[$key]) and is_array($val)) 
            {
                $max2 = max_by_key($val, $key);
                $max[$max2] = 1;
            } 
            elseif (!isset($val[$key]) and !is_array($val)) 
            {
                return false;
            } 
            elseif (isset($val[$key])) 
            {
                $max[$val[$key]] = 1;
            }
        }
        return max( array_keys($max) );
    }