简体   繁体   中英

Return number in array that is greater than X

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?

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

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

Assuming a standard sorted array, ie 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM