簡體   English   中英

在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張,因此需要使用關鍵字key從陣列中刪除/過濾一張)。 這是為了避免向用戶發送過多的促銷標語。

2)

使用promo關鍵字隨機排列任何圖像的位置,但仍通過insertDate保持標准關鍵字的圖像順序。

考慮到這些結果集的大小總是會有所不同,以編程方式實現這一目標的最佳方法是什么?

編輯:我創建了以下功能來解決第一部分:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM