1

这个问题有点像我之前的一个问题

但是,我想用最基本的术语来说明这一点,希望有人能够帮助我找到解决方案。

考虑这个多维数组:

$images = array(
    0 = array(
        'height' => 100
    ),
    1 = array(
        'height' => 90
    ),
    2 = array(
        'height' => 60
    ),
    3 = array(
        'height' => 30
    )
);

请注意,数组已经按高度降序排序!

这些图像需要被推入三列,同时尽量保持列的高度。因此(上述示例的)结果数组将如下所示:

$gallery_columns = array(
    1 => array(
        'height' => 100
        'images' => array(
            0 = array(
                'height' => 100
            ),
        )
    ),
    2 => array(
        'height' => 90
        'images' => array(
            0 = array(
                'height' => 90
            ),
        )
    ),
    3 => array(
        'height' => 90
        'images' => array(
            0 = array(
                'height' => 60
            ),
            1 = array(
                'height' => 30
            ),
        )
    )
);

我正在努力编写一个算法来实现上述目标。任何人都可以在不使用大量嵌套循环等的情况下看到实现这一目标的好方法吗?

4

1 回答 1

1

这可能是您正在寻找的:

$images = array(
    0 => array(
        'height' => 100
    ),
    1 => array(
        'height' => 90
    ),
    2 => array(
        'height' => 60
    ),
    3 => array(
        'height' => 30
    )
);

$gallery_columns = array(
    1 => array(
        'height' => 0,
        'images' => array()
    ),
    2 => array(
        'height' => 0,
        'images' => array()
    ),
    3 => array(
        'height' => 0,
        'images' => array()
    )
);



foreach ($images as $img)
{
    // start with first column as the shortest
    $shortest_colm = &$gallery_columns[1];

    // loop over all the columns, updating the shortest column if shorter
    for ($i = 2; $i <= 3; $i++)
    {
        if ($gallery_columns[$i]['height'] < $shortest_colm['height'])
        {
            $shortest_colm = &$gallery_columns[$i];
        }
    }

    // push the image into the shortest column
    $shortest_colm['height'] += $img['height'];
    $shortest_colm['images'][] = $img;
}

print_r($gallery_columns);

输出:

Array
(
    [1] => Array
        (
            [height] => 100
            [images] => Array
                (
                    [0] => Array
                        (
                            [height] => 100
                        )

                )

        )

    [2] => Array
        (
            [height] => 90
            [images] => Array
                (
                    [0] => Array
                        (
                            [height] => 90
                        )

                )

        )

    [3] => Array
        (
            [height] => 90
            [images] => Array
                (
                    [0] => Array
                        (
                            [height] => 60
                        )

                    [1] => Array
                        (
                            [height] => 30
                        )

                )

        )

)

奖金:

foreach($gallery_columns as &$column)
{
    shuffle($column['images']);
}

由于您按从大到小的顺序插入图像,因此您可以在插入完成后随机播放图像。这样它就不会在顶部有所有大图像,在底部有小图像。

于 2013-01-02T09:41:46.387 回答