Php 将图像数组排序到库列中

Php 将图像数组排序到库列中,php,image,gallery,sinemacula,Php,Image,Gallery,Sinemacula,这个问题是从我先前的一个问题延续下来的 然而,我想用最基本的术语来说明这一点,希望有人能帮助我找到解决办法 考虑这个多维数组: $images = array( 0 = array( 'height' => 100 ), 1 = array( 'height' => 90 ), 2 = array( 'height' => 60 ), 3 = array( '

这个问题是从我先前的一个问题延续下来的

然而,我想用最基本的术语来说明这一点,希望有人能帮助我找到解决办法

考虑这个多维数组:

$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
            ),
        )
    )
);

我正在努力编写一个算法来实现上述目标。有人能想出一个不使用大量嵌套循环等实现这一点的好方法吗?

这可能就是您想要的:

$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);

奖金:

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

因为您是按从大到小的顺序插入图像的,所以插入完成后,您可以对图像进行洗牌。这样,它就不会将所有的大图像都放在顶部,而将小图像放在底部。

这可能就是您要寻找的:

$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);

奖金:

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

因为您是按从大到小的顺序插入图像的,所以插入完成后,您可以对图像进行洗牌。这样它就不会把所有的大图像都放在顶部,而把小图像放在底部。

嘿,本,你要找的是某个宽度吗?另外,图像的高度范围是多少?:)快乐的后启示录@JoshBrody图像的宽度总是相同的,并且列的高度与需要的高度一样高。这取决于数组中图像的数量,页面将具有无限滚动。更多细节见我的另一个问题嘿Ben,你有你想要的宽度吗?另外,图像的高度范围是多少?:)快乐的后启示录@JoshBrody图像的宽度总是相同的,并且列的高度与需要的高度一样高。这取决于数组中图像的数量,页面将具有无限滚动。查看我的另一个问题了解更多详细信息非常感谢,我本来打算实现shuffle函数,问题是,我希望它们总是以相同的顺序显示。有没有一种方法可以改变顺序,但要确保始终是相同的。我知道那个句子自相矛盾,但你应该明白我的意思:-)。最明显的方法是只按“date created”(创建日期)之类的内容进行排序,但这意味着从文件中检索创建的日期,这在处理大量图像时可能会很昂贵……是的,使用。只需将种子值设为常量,并假设您有相同的图像,它们将始终以相同的顺序进行洗牌。非常感谢,我本来打算实现洗牌功能,问题是,我希望它们始终以相同的顺序显示。有没有一种方法可以改变顺序,但要确保始终是相同的。我知道那个句子自相矛盾,但你应该明白我的意思:-)。最明显的方法是只按“date created”(创建日期)之类的内容进行排序,但这意味着从文件中检索创建的日期,这在处理大量图像时可能会很昂贵……是的,使用。只需将种子值设为常量,并假设您拥有相同的图像,它们将始终按照相同的顺序进行洗牌。