如何使用 PHP 动态创建价格范围 [英] How to create Price Range dynamically with PHP

查看:51
本文介绍了如何使用 PHP 动态创建价格范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从价格数组创建价格范围?假设我有这个包含价格的数组:

How can I create Price Ranges from price array? Let's say I have this array which holds prices:

  Array ( [0] => 500 [1] => 500 [2] => 520 [3] => 540 [4] => 551 [5] => 599 [6] => 601 [7] => 601 [8] => 650 [9] => 681 [10] => 750 [11] => 750 [12] => 851 [13] => 871 [14] => 871 [15] => 900 [16] => 990 [17] => 999 [18] => 1101 [19] => 1130 [20] => 1149 [21] => 1151 [22] => 1278 [23] => 1300 [24] => 1460 ) 

最小值 = 500,最大值为 1460.我需要像这样向用户展示:

Minimum value is = 500 and maximum value is 1460. I need to show users like this :

[x] 500-750 (11)
[x] 750-1000 (8)
[x] 1000+ (7)

棘手的部分是如果值达到 1500 或超过 1500 需要看起来像这样:

the tricky part is if values are reached 1500 or there are more than 1500 above needs look like this :

[x] 500-750 (11)
[x] 750-1000 (8)
[x] 1000-1500 (n)
[x] 1500+ (if more than 1500 but not reached 2000).

我们可以说 1500 是极限,但如果价格在 1500 美元和 2000 美元之间,并且价格超过 2000 美元,例如 2300 美元、2400 美元、2499 美元、2000 美元+ 必须是极限.看起来像这样:

we can say 1500 is the limit but if there are prices between 1500$ and 2000$ and more than 2000$ like 2300$ , 2400$ , 2499$, 2000$+ must be the limit. which will look like this:

[x] 500-750 (11)
[x] 750-1000 (8)
[x] 1000-1500 (n)
[x] 1500-2000 (n)
[x] 2000$+ (n)

这将创建 5 个范围.如您所见,最低价格为 500,但最低.价格可能为 20 美元或 120 美元,因此函数不得违反 5 范围规则.第一个范围可以是 20$-750$ (n).

This will create 5 range. As you can see lowest price is 500, but min. prices may 20$ or 120$ so function must not break 5 range rule. first range can be 20$-750$ (n).

当然,最高价格可能不会超过 1000 美元,因此可能需要显示如下内容:

And of course max prices may not 1000$+ so may need to show something like this :

[x] 25-50 (11)
[x] 50-100(8)
[x] 100-200(n)
[x] 200-400(n)
[x] 400+ (n)

我希望我能做到这一点.条件很多,我不知道php中pow或range之类的函数的用法.希望你能帮上忙:/

I hope I make this proper. There are many conditions and I don't know the usages of pow or range like functions in php. Hope you can help :/

推荐答案

我根据问题的 $array 元素创建了这个函数.

I Created this function based on my $array element of my question.

如果这是我的问题的好方法,请提出一些评论,例如指出我的错误.

If this is a good aproach of my question please make some comments for like pointing my mistakes.

http://laravel.io/bin/VLJn 这是输出.

private function createRange($array){
    sort($array);

    //Setting range limits.
    //Check if array has 5 digit number.
    $countDigitedNumbers = preg_grep('/\d{5}/',$array);
    if(count($countDigitedNumbers) > 3){
        $rangeLimits = array(0,1000,2500,5000,10000,15000,20000,25000);
    }else{
        $rangeLimits = array(0,50,250,500,1000,1500,2000,2500);
    }
    $ranges = array();

    for($i = 0; $i < count($rangeLimits); $i++){
        if($i == count($rangeLimits)-1){
            break;
        }
        $lowLimit = $rangeLimits[$i];
        $highLimit = $rangeLimits[$i+1];

        $ranges[$i]['ranges']['min'] = $lowLimit;
        $ranges[$i]['ranges']['max'] = $highLimit;

        foreach($array as $perPrice){
            if($perPrice >= $lowLimit && $perPrice < $highLimit){
                $ranges[$i]['values'][] = $perPrice;
            }
        }
    }
    return $ranges;
}

这篇关于如何使用 PHP 动态创建价格范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆