1

我有这个数组:

$myArray = array("0x40x40" => 64, "0x50x40" => 65, "0x60x40" => 66);

现在我想找到最小的值,在本例中是 64(第一个键/值对)。除了循环遍历数组并比较值之外,还有其他方法吗?最小值并不总是第一个,并且这些值也不是按顺序排序的。

谢谢!

4

5 回答 5

4

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
于 2012-07-26T09:41:05.833 回答
2

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);

http://codepad.org/NXhfZpBm

于 2012-07-26T09:41:16.007 回答
1

Use below statement in your code and you are done.

min($myArray);
于 2012-07-26T09:42:45.400 回答
0

如果您只需要最低或最高值

$myArray = array( "0x40x40" => 64, "0x50x40" => 65, "0x60x40" => 66 ); 
asort ( $myArray );
$item = current( $myArray ); 

这是从最低到最高,相反 U 需要 arsort() 函数

于 2012-07-26T09:50:54.247 回答
0

将返回 #array('0x40x40')

$myArray = array(
        "0x40x40" => 64, 
        "0x50x40" => 65, 
        "0x60x40" => 66, 
        );
    array_keys($myArray, min($myArray));  
于 2012-07-26T09:45:07.000 回答