我需要找到与变量值对应的基本数组的左 -near键。
搜索值(在这种情况下)始终在1到779之间
更好的例子:
$fixedArr = [ 0, 5, 8, 20, 40, 60, 90, 135, 780 ];
$search = 42; // $result = $arr[4] -> 4;
$search = 110; // $result = $arr[6] -> 6;
$search = 134; // $result = $arr[6] -> 6;
$search = 135; // $result = $arr[7] -> 7;
我尝试使用foreach循环,但没有运气,任何想法? 感谢
答案 0 :(得分:1)
这可能对你有帮助;
$fixedArr = [ 0, 5, 8, 20, 40, 60, 90,135,780 ];
//
$search = 111; // $result = $arr[4] -> 4;
//$search = 110; // $result = $arr[6] -> 6;
//
function leftORright($fixedArr,$search){
$max = max($fixedArr)+1;
$near = array(
'left'=>array('key'=>'none','value'=>'none','bool'=>false),
'right'=>array('key'=>'none','value'=>$max,'bool'=>false),
'center'=>array('key'=>'none','value'=>'none','bool'=>false)
);
foreach($fixedArr as $k=>$v){
if($v == $search){
$near['center']['key'] = $k;
$near['center']['value'] = $v;
}
if($v < $search){
$near['left']['key'] = $k;
$near['left']['value'] = $v;
}
if($v > $search and $near['right']['value'] > $v){
$near['right']['key'] = $k;
$near['right']['value'] = $v;
}
}
//decide near left or right
$respright = $near['right']['value'] - $search;
$respleft = $search - $near['left']['value'] ;
$right_left_equals = false;
if($near['center']['value'] !== 'none'){
$near['center']['bool'] = true;
}else if($respleft < $respright && $near['left']['key']!='none'){
$near['left']['bool'] = true;
}else if($respleft > $respright && $near['right']['key']!='none'){
$near['right']['bool'] = true;
}else if($near['center']['value'] != 'none'){
$near['center']['value'] = true;
}else{
$right_left_equals = true;
}
//var_dump($near);
//Result is:
foreach($near as $k=>$v){
foreach($v as $k2=>$v2){
if($v2===true){
var_dump('near is for '.$k);
return $v;
}
}
}
//equal for right and left
if($right_left_equals){
var_dump('near right left are equals');
return array($near['right'],$near['left']);
}
}
$result = leftORright($fixedArr,$search);
var_dump($result);
响应:
string 'near is for left' (length=16)
array (size=3)
'key' => int 6
'value' => int 90
'bool' => boolean true
答案 1 :(得分:1)
搜索值始终介于(在本例中)1和779
之间$fixedArr = [ 0, 5, 8, 20, 40, 60, 90, 135, 780 ];
$search = 42;
for ($i = 0; $i < count($fixedArr); $i++)
if ($search < $fixedArr[$i]) break;
echo $i-1;
答案 2 :(得分:0)
使用array_search
:
array_search
- 在数组中搜索给定值,并在成功时返回相应的键
$search = array_search(42, $fixedArr); // -> 4
$search = array_search(110, $fixedArr); // -> 6
您的问题类似于找到最近的问题:
<?php
$fixedArr = [ 0, 5, 8, 20, 40, 60, 90, 135, 780 ];
function getClosest($search, $arr) {
$left = 0;
foreach ($arr as $val) {
if ($search > $val)
$left = $val;
elseif ($search < $val) {
$right = $val;
break;
}
else {
$right = $val;
break;
}
}
return array($left, $right, array_search($left, $arr), array_search($right, $arr), (($search - $left) > ($right - $search) ? array_search($right, $arr) : array_search($left, $arr)));
}
print_r(getClosest(4, $fixedArr));
?>
答案 3 :(得分:0)
试试这个:它适用于所有情况。
$fixedArr = [ 0, 5, 8, 20, 40, 60, 90, 135, 780 ];
$find = 14;
for ($i=0; $i < count($fixedArr); $i++) {
if($fixedArr[$i] <= $find){
$large[] = $fixedArr[$i];
}else{
$small[] = $fixedArr[$i];
}
}
$near1 = max($large);
$near2 = min($small);
echo "Value $find coming in between $near1 and $near2";
echo "<br>";
if($find >= ($near1 + $near2) / 2){
echo "Closed Value is : $near2";
}else{
echo "Closed Value is : $near1";
}
输出:
Value 14 coming in between 8 and 20
Closed Value is : 20