如果数组键之间的值

时间:2015-11-14 23:51:50

标签: php

示例我有这个数组:

$ar = array(
    "1.00" => array("value0"," very bad"),
    "1.49" => array("value1","bad"),
    "2.00" => array("value2","not bad"),
    "2.49" => array("value3","normal"),
    "3.00" => array("value4","good"),
    "3.49" => array("value5","very good")
 );

我想检查$ val是否低于1.00 $ result is array(“value0”,“非常糟糕”)。如果在1.00 - 1,49范围之间,则结果为数组(“value1”,“bad”)等。 有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:1)

这是一个提示:

<?php

$ar = array(
    "1.00" => array("value0"," very bad"),
    "1.49" => array("value1","bad"),
    "2.00" => array("value2","not bad"),
    "2.49" => array("value3","normal"),
    "3.00" => array("value4","good"),
    "3.49" => array("value5","very good")
 );


$input = 1.2;

foreach($ar as $key=>$text)
{
  if($input < floatval($key))
    {
      echo $text[0].' => '.$text[1];
      break;
    }
}

?>

答案 1 :(得分:0)

$val = '2.15';
$val_data = ['value2','not bad'];

$data = array(
    "1.00" => array("value0"," very bad"),
    "1.49" => array("value1","bad"),
    "2.00" => array("value2","not bad"),
    "2.49" => array("value3","normal"),
    "3.00" => array("value4","good"),
    "3.49" => array("value5","very good")
);

$_fkey = array_keys($data)[0];
foreach($data as $key => $value){
    if($key > $_fkey && $key < $val){$_fkey = $key;}
}

echo "$val, $val_data\n";
echo "=> $f_key, " . $data[$f_key] . "\n";