我有这个数组:
$myArray = array("0x40x40" => 64, "0x50x40" => 65, "0x60x40" => 66);
现在我想找到最小的值,在本例中是 64(第一个键/值对)。除了循环遍历数组并比较值之外,还有其他方法吗?最小值并不总是第一个,并且这些值也不是按顺序排序的。
谢谢!
You can use the min() function to get your answer nicely.
echo min(2, 3, 1, 6, 7); // 1
or
$myArray=array(2, 3, 1, 6, 7);
echo min($myArray); // 1
Using min:
$myArray = array(
"0x40x40" => 64, "0x50x40" => 65, "0x60x40" => 66, "0x70x40" => 67, "0x80x40" => 68, "0x90x40" => 70, "0x100x40" => 71, "0x110x40" => 74, "0x120x40" => 76);
echo min($myArray);
Use below statement in your code and you are done.
min($myArray);
如果您只需要最低或最高值
$myArray = array( "0x40x40" => 64, "0x50x40" => 65, "0x60x40" => 66 );
asort ( $myArray );
$item = current( $myArray );
这是从最低到最高,相反 U 需要 arsort() 函数
将返回 #array('0x40x40')
$myArray = array(
"0x40x40" => 64,
"0x50x40" => 65,
"0x60x40" => 66,
);
array_keys($myArray, min($myArray));