使用PHP在数组中获取匹配或下一个最接近的较大值。

huangapple go评论47阅读模式
英文:

Get matching or next closest larger value in an array using PHP

问题

如何在数组中找到或搜索下一个最接近的较大数?

例如,假设以下是我的数组

array(10, 8, 6, 12, 4, 20)

当我搜索 9 时,它应该从上面的数组中给我 10

如果我搜索 13,它应该给我 20

如果我搜索 5,它应该给我 6

等等

英文:

How can I find or search for the next closest larger number in an array?

For example, let's assume the following is my array

array( 10, 8, 6, 12, 4, 20 )

when I search for 9, it should give me 10 from the above array

if I search for 13 it should give me 20

if I search for 5 it should give me 6

and so on

答案1

得分: 2

在排序与遍历整个数组之间存在计算方面的权衡。我不是计算机科学专业的,如果我必须在某个地方消除微秒,我会进行基准测试,但它运行正常。

function closest($array, $number)
{
    sort($array);
    foreach ($array as $a) {
        if ($a >= $number) return $a;
    }
    return end($array);
}

$array = array( 10, 8, 6, 12, 4, 20 );

echo closest($array, 9); // 10
echo closest($array, 13); // 20
echo closest($array, 5); // 6
英文:

There's a tradeoff in compute in sorting versus iterating over the whole thing. I'm not a compsci major and if I had to eliminate microseconds somewhere I'd benchmark this but it works fine.

function closest($array, $number)
{
    sort($array);
    foreach ($array as $a) {
        if ($a >= $number) return $a;
    }
    return end($array);
}

$array = array( 10, 8, 6, 12, 4, 20 );

echo closest($array, 9); // 10
echo closest($array, 13); // 20
echo closest($array, 5); // 6

答案2

得分: 0

Also figured out a new way of solving it Great to @Daniel Tharp for the great answer. What I did was to loop through each element of an array, and picked all the numbers that are bigger than the search then pushed them into a fresh new array. Finally, I used PHP min to get the least number from the newly created array.

Be careful when using min as it can trigger an error if no match was found in an array. See the solution below.

function wpccb_get_next_closest_large_number( $search, $array ){
  $array_of_all_larger_numbers = array();
  foreach( $array as $value ){
    if( $search < $value ){
        array_push( $array_of_all_larger_numbers, $value );
    }
  }
  
  if( count( $array_of_all_larger_numbers ) == 0 ){
    return 0;
  }
  
  return min( $array_of_all_larger_numbers );
}
英文:

Also figured out a new way of solving it Great to @Daniel Tharp for the great answer. What I did was to loop through each element of an array, and picked all the numbers that are bigger than the search then pushed them into a fresh new array. Finally, I used PHP min to get the least number from the newly created array.

Be careful when using min as it can trigger an error if no match was found in an array. See the solution below.

function wpccb_get_next_closest_large_number( $search, $array ){
  $array_of_all_larger_numbers = array();
  foreach( $array as $value ){
    if( $search < $value ){
        array_push( $array_of_all_larger_numbers, $value );
    }
  }
  
  if( count( $array_of_all_larger_numbers ) == 0 ){
    return 0;
  }
  
  return min( $array_of_all_larger_numbers );
}

huangapple
  • 本文由 发表于 2023年6月12日 07:53:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定