在PHP中过滤和排序多维数组


Filter and Sort a multidimensional array in PHP

我正在建立一个图片库,并想在随机点投放一些促销横幅,向用户推广某些优惠。给定以下数组来自数据库查询:

    Array
    (
        [0] => Array
            (
                [insertDate] => 2014-11-10 11:22:58
                [keyword] => standard
                [mediaClass] => image
                [mediaURL] => http://image1.jpg
                [promoURL] => 
            )
        [1] => Array
            (
                [insertDate] => 2014-11-10 11:23:08
                [keyword] => promo
                [mediaClass] => image
                [mediaURL] => http://image2.jpg
                [promoURL] => http://www.google.com
            )
        [2] => Array
            (
                [insertDate] => 2014-11-10 11:23:18
                [keyword] => standard
                [mediaClass] => image
                [mediaURL] => http://image3.jpg
                [promoURL] => 
            )
        [3] => Array
            (
                [insertDate] => 2014-11-10 11:23:28
                [keyword] => standard
                [mediaClass] => image
                [mediaURL] => http://image4.jpg
                [promoURL] => 
            )
        [4] => Array
            (
                [insertDate] => 2014-07-08 11:23:38
                [keyword] => promo
                [mediaClass] => image
                [mediaURL] => http://image5.jpg
                [promoURL] => http://www.google.com
            )
        [5] => Array
            (
                [insertDate] => 2014-07-08 11:23:48
                [keyword] => standard
                [mediaClass] => image
                [mediaURL] => http://image6.jpg
                [promoURL] => 
            )
     )

我试着做两件事;

1)

确保每个5个标准图像只有一个促销图片(在本例中有2个,因此需要使用关键字键从数组中删除/筛选一个)。这是为了避免向用户发送过多的促销横幅。

2)

Ramdomise任何带有promo关键字的图像的位置,但通过insertDate仍然保持带有standard关键词的图像的顺序。

考虑到这些结果集的大小总是不同的,通过编程实现这一点的最佳方式是什么?

编辑:我创建了以下函数来处理第一部分:

function limitMediaPromos($array, $key, $value, $limit)
{
    // count the number of promo images
    if($count = $this->countElementsWithValue($array, $key, $value))
    {
        // find the avg number of promos per media set
        $avg = floor((count($array) / $count));
        // remove promo element if avg is over set limit
        if($avg > $limit)
        {
            $array = $this->removeElementWithValue($array, $key, $value);
        }
    }
    return $array;
}

为了在数据库端获得更好的性能排序,请删除mediaSort函数和usort($medias,'mediaSort');在下面

function isMedia($var)  {
    return empty($var['promoURL']);
}
function isPromo($var)
{
    return !empty($var['promoURL']);
}
function mediaSort($mediaOne, $mediaTwo)
{
    return (strtotime($mediaOne['insertDate']) <= strtotime($mediaTwo['insertDate']));
}
function getImages($input)
{
    // Get the promos. O(n).
    $promos = array_filter($input, "isPromo");
    // randomize them. O(n).
    shuffle($promos);
    // Get the medias. O(n)
    $medias = array_filter($input, "isMedia");
    // Sort them (ideally sort it on the database side and skip this step). O(n log n)
    usort($medias, 'mediaSort');
    $promoOdds = 1/5;
    $promoGap = 5;
    $returnIndex = 0;
    $return = array();
    foreach ($medias as $media) {
        if (is_null($lastPromoIndex)
            || $returnIndex > $lastPromoIndex + $promoGap
        ) {
            if (rand(1,1/$promoOdds) == 1) {
                $return[] = array_pop($promos);
                $lastPromoIndex = $returnIndex;
            }
        }
        $return[] = $media;
        $returnIndex++;
    }
    if (is_null($lastPromoIndex)
        || $returnIndex > $lastPromoIndex + $promoGap
    ) {
        $return[] = array_pop($promos);
    }
    return $return;
}
print_r(getImages($input));