获取PHP数组中的最小值并获取相应的键

时间:2021-02-19 21:19:29

I have an array, Array ( [0] => 3 [1] => 0 ). I want PHP code that returns 1 because 1's value is the lowest. How do I do this? This is for the code at https://github.com/timothyclemans/RoboQWOP/commit/e205401a56b49e8b31f089aaee0042f8de49a47d

我有一个数组,Array([0] => 3 [1] => 0)。我希望PHP代码返回1,因为1的值是最低的。我该怎么做呢?这是代码https://github.com/timothyclemans/RoboQWOP/commit/e205401a56b49e8b31f089aaee0042f8de49a47d

4 个解决方案

#1


12  

array_keys($array, min($array));

#2


23  

This will return the first index that has the minimum value in the array. It is useful if you only need one index when the array has multiple instances of the minimum value:

这将返回数组中具有最小值的第一个索引。如果在阵列具有多个最小值实例时只需要一个索引,那么它很有用:

$index = array_search(min($my_array), $my_array);

This will return an array of all the indexes that have the minimum value in the array. It is useful if you need all the instances of the minimum value but may be slightly less efficient than the solution above:

这将返回数组中具有最小值的所有索引的数组。如果您需要所有最小值的实例,但可能效率略低于上述解决方案,则非常有用:

$index = array_keys($my_array, min($my_array));

#3


2  

http://php.net/manual/en/function.min.php

http://php.net/manual/en/function.array-search.php

$array = array( [0] => 3, [1] => 0);
$min = min($array);
$index = array_search($min, $array);

Should return 1

应该返回1

#4


1  

The below example would help you.

以下示例可以帮助您。

$values=array(3,0,4,2,1);
$min_value_key=array_keys($values, min($values));
echo $min_value_key;

Hope this helps.

希望这可以帮助。

#1


12  

array_keys($array, min($array));

#2


23  

This will return the first index that has the minimum value in the array. It is useful if you only need one index when the array has multiple instances of the minimum value:

这将返回数组中具有最小值的第一个索引。如果在阵列具有多个最小值实例时只需要一个索引,那么它很有用:

$index = array_search(min($my_array), $my_array);

This will return an array of all the indexes that have the minimum value in the array. It is useful if you need all the instances of the minimum value but may be slightly less efficient than the solution above:

这将返回数组中具有最小值的所有索引的数组。如果您需要所有最小值的实例,但可能效率略低于上述解决方案,则非常有用:

$index = array_keys($my_array, min($my_array));

#3


2  

http://php.net/manual/en/function.min.php

http://php.net/manual/en/function.array-search.php

$array = array( [0] => 3, [1] => 0);
$min = min($array);
$index = array_search($min, $array);

Should return 1

应该返回1

#4


1  

The below example would help you.

以下示例可以帮助您。

$values=array(3,0,4,2,1);
$min_value_key=array_keys($values, min($values));
echo $min_value_key;

Hope this helps.

希望这可以帮助。