我有一个数组,我需要得到密钥的最低值。因为我需要检查值是否包含正确的键
Array (
[120] => 97.00
[132] => 92.67
[124] => 72.33
[131] => 49.67
[129] => 25.00
[127] => 25.00 //<--Get the value and this key
)
//This array already sorted, no need to sort just get the last value and key
如何在密钥[127]
中获得最低值25.00 $array = array (
"120" => 97.00
"132" => 92.67
"124" => 72.33
"131" => 49.67
"129" => 25.00
"127" => 25.00
);
print_r(min($array)); //<--displaying only the value but not the key
输出应该像
127 - 25.00
答案 0 :(得分:4)
$array=array("120" =>97.00 ,"132"=>92.67,"124"=>72.33 ,"131"=>49.67,"129"=>29.00,"127"=> 25.00);
//get minimum value from array
$minvalue=(min($array));
echo "Min Value= ".$minvalue;
//get key of minimum value from array
$key = array_search($minvalue, $array);
echo "Key of Min Value".$key;
// make new associative array
$newarray = array($key => $minvalue );
print_r($newarray);
答案 1 :(得分:1)
$array = array (
"120" => 97.00,
"132" => 92.67,
"124" => 72.33,
"131" => 49.67,
"129" => 25.00,
"127" => 25.00
);
// point the last element of the element and fetch value to $value
$value= (end($array));
// fetch key
echo "key : ". key($array);
echo "value :".$value;
答案 2 :(得分:0)
尝试使用asort,然后获取第一个元素的键和值
//using asort will preserve key value pair sort will happen on value
asort($urarray);
foreach($urarray as $k=>$val){
//read the first element and break
echo $k.' - '.floatval($val);
break;
}
有关asort的更多信息,请阅读此php asort
答案 3 :(得分:-1)
PHP手册加载了很多东西。
排序:http://php.net/manual/en/function.asort.php
目前:http://php.net/manual/en/function.current.php
键:http://php.net/manual/en/function.key.php
<?php
$array = array (
"120" => 97.00 ,
"132" => 92.67 ,
"124" => 72.33 ,
"131" => 49.67 ,
"129" => 25.00 ,
"127" => 25.00
);
asort($array);
$key = key($array);
$value = current($array);
echo "$key - $value";
?>
您也可以使用arsort()然后执行array_pop()。取决于你想去哪个方向。