Return number in array that is greater than X

旧街凉风 提交于 2020-01-25 02:20:31

问题


The following code automagically finds the highest price on a page:

    $vw_link = get_field('shop_link');

    $ch = curl_init($vw_link);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $cl = curl_exec($ch);

    $dom = new DOMDocument();
    @$dom->loadHTML($cl);
    $xpath = new DOMXpath($dom);

    $price = $xpath->query("//span[@class='price']");

    foreach($price as $value) { 
    $vw_array[] =  floatval(str_replace('$', '', $value->nodeValue));
    update_field('shop_price',max($vw_array));
    }

What would be the best thing since sliced cheese is if it could return the value in $vw_array that is between a specific amount, ie, greater than 100 and less than 200. The tricky part would be returning the first greatest number after 100, if there are multiple numbers between 100-200.

For example (if the following prices are all wrapped in 'price' class):

$88

$92

$105  <-- return this number

$125

$180

$210

Does anyone know how to conjure up such a magnificent function?


回答1:


function getHighestValue($arr, $high = 200, $low =100) {
    $data = $arr;
    // sort it so you can find 1st highest value in range
    asort($data);

    foreach ($data as $item) {
    //trim dollar sign as i can see from your example
        $item = ltrim($item, "$");
        if ($item >= $low && $item <= $high) {
            return $item;
        }
    }
  return false;
}

So if your data is

$arr = [
    '$140',
    '$22',
    '$143',
    '$199',
];

var_dump(getHighestValue($arr)); // will return 140

Also you can add dollar sign back after that




回答2:


I think if I understand you correctly, this should work. You can strip out your $ if it's there.

function ReturnFirst($array = array(), $low = 0, $high = 0) {
        foreach($array as $value) {
                if(($value >= $low) && ($value <= $high)) {
                        // When first number hit, just return it
                        return $value;
                    }
            }
        // This will return false if it doesn't return any results
        return false;
    }

echo
$number =   ReturnFirst(array(10,20,30,40,50,60), 35,60);

// This will return the number 40



回答3:


Assuming a standard sorted array, i.e. elements in ascending or descending order with indices 0, 1, 2, ..., the fastest way for a larger array would be the binary search algorithm.

// assuming sorted $a, looking for $value as lowest value
$low = 0;
$high = count($a);
while ($low < $high) {
  $middle = (($high - $low) >> 1) + $low; // same as floor(($high - $low) / 2)
  if ($a[$middle] < $value) // or: strcmp($a[$middle], $value) < 0
    $low = $middle + 1;
  else
    $high = $middle;
}

At the end the $low variable will contain the earliest possible insert index for $value in the sorted array. In other words, if $low is less than the array's length, it will point at the value you want, if it is less than your upper value bound. Otherwise the arrays contains no value that matches your request.

However, if you only deal with shorter arrays, linear search may be faster due to its reduced complexity.




回答4:


Code:

$Max = 15;
$Min = 10;

$A = ['$6','$7','$9','$11','$12','$13','$17','$22']; // Array in

foreach($A As $V){
    $B = ltrim($V, "$");

    if($B >= $Min && $B <= $Max) {
        $C[] = $V;
    }
}

print_r($C);

Output:

$11
$12
$13


来源:https://stackoverflow.com/questions/27997309/return-number-in-array-that-is-greater-than-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!