php 找出最接近的数字,在数组中查找匹配或最接近的值

这是我为排序的大数组编写的高性能函数

对于具有20000个元素的数组,经过测试的主循环仅需要~20次迭代。

请注意数组必须排序(升序)!

define('ARRAY_NEAREST_DEFAULT',    0);

define('ARRAY_NEAREST_LOWER',      1);

define('ARRAY_NEAREST_HIGHER',     2);

/**

* Finds nearest value in numeric array. Can be used in loops.

* Array needs to be non-assocative and sorted.

*

* @param array $array

* @param int $value

* @param int $method ARRAY_NEAREST_DEFAULT|ARRAY_NEAREST_LOWER|ARRAY_NEAREST_HIGHER

* @return int

*/

function array_numeric_sorted_nearest($array, $value, $method = ARRAY_NEAREST_DEFAULT) {

$count = count($array);

if($count == 0) {

return null;

}

$div_step               = 2;

$index                  = ceil($count / $div_step);

$best_index             = null;

$best_score             = null;

$direction              = null;

$indexes_checked        = Array();

while(true) {

if(isset($indexes_checked[$index])) {

break ;

}

$curr_key = $array[$index];

if($curr_key === null) {

break ;

}

$indexes_checked[$index] = true;

// perfect match, nothing else to do

if($curr_key == $value) {

return $curr_key;

}

$prev_key = $array[$index - 1];

$next_key = $array[$index + 1];

switch($method) {

default:

case ARRAY_NEAREST_DEFAULT:

$curr_score = abs($curr_key - $value);

$prev_score = $prev_key !== null ? abs($prev_key - $value) : null;

$next_score = $next_key !== null ? abs($next_key - $value) : null;

if($prev_score === null) {

$direction = 1;

}else if ($next_score === null) {

break 2;

}else{

$direction = $next_score < $prev_score ? 1 : -1;

}

break;

case ARRAY_NEAREST_LOWER:

$curr_score = $curr_key - $value;

if($curr_score > 0) {

$curr_score = null;

}else{

$curr_score = abs($curr_score);

}

if($curr_score === null) {

$direction = -1;

}else{

$direction = 1;

}

break;

case ARRAY_NEAREST_HIGHER:

$curr_score = $curr_key - $value;

if($curr_score < 0) {

$curr_score = null;

}

if($curr_score === null) {

$direction = 1;

}else{

$direction = -1;

}

break;

}

if(($curr_score !== null) && ($curr_score < $best_score) || ($best_score === null)) {

$best_index = $index;

$best_score = $curr_score;

}

$div_step *= 2;

$index += $direction * ceil($count / $div_step);

}

return $array[$best_index];

}

ARRAY_NEAREST_DEFAULT 找到最近的元素

ARRAY_NEAREST_LOWER 找到最近的元素是LOWER

ARRAY_NEAREST_HIGHER 找到最接近的元素

用法:

$test = Array(5,2,8,3,9,12,20,...,52100,52460,62000);

// sort an array and use array_numeric_sorted_nearest

// for multiple searches.

// for every iteration it start from half of chunk where

// first chunk is whole array

// function doesn't work with unosrted arrays, and it's much

// faster than other solutions here for sorted arrays

sort($test);

$nearest = array_numeric_sorted_nearest($test, 8256);

$nearest = array_numeric_sorted_nearest($test, 3433);

$nearest = array_numeric_sorted_nearest($test, 1100);

$nearest = array_numeric_sorted_nearest($test, 700);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值