Get the minimum value in the PHP array and get the corresponding key

I have an array, Array ([0] => 3 [1] => 0). I want the PHP code to return 1 because the value 1 is the lowest. How can I do it? This is for the code https://github.com/timothyclemans/RoboQWOP/commit/e205401a56b49e8b31f089aaee0042f8de49a47d

+4
source share
4 answers
array_keys($array, min($array)); 
+13
source

This will return the first index that has the minimum value in the array. This 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 indexes that have the minimum value in the array. This is useful if you need all instances of the minimum value, but may be slightly less efficient than the solution above:

 $index = array_keys($my_array, min($my_array)); 
+20
source

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

Must be returning 1

+2
source

The following example will 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
source

All Articles