【问题标题】:Filter and Sort a multidimensional array in PHP在 PHP 中过滤和排序多维数组
【发布时间】:2014-11-10 04:17:56
【问题描述】:

我正在构建一个图片库,并希望在随机点投放一些促销横幅,以向用户宣传某些优惠。鉴于以下数组来自数据库查询:

    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 张标准 图片只有一张 promo 图片(在此示例中,有 2 张图片,因此需要使用关键字从数组中删除/过滤一张图片钥匙)。这是为了避免向用户发送过多的促销横幅。

2)

使用 promo 关键字随机化任何图像的位置,但仍使用 standard 关键字在 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;
}

【问题讨论】:

  • 好要求,你在哪里试用?
  • 嗨,Ghost,检查编辑,此代码到目前为止有效,然后我需要按 insertDate 值排序。

标签: php arrays multidimensional-array array-filter array-splice


【解决方案1】:

为了在数据库端进行更好的排序,删除了 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));

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-03-27
  • 1970-01-01
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
  • 1970-01-01
  • 2014-02-14
  • 1970-01-01
相关资源
最近更新 更多