Hướng dẫn get min index php
I have an array like this: Show
Nội dung chính
I need to extract minimal and maximal Weight values. In this example $min_value = 175 $max_value = 200 Any help on how to do this ? Thank you ! asked May 1, 2011 at 3:06 2 Option 1. First you map the array to get those numbers (and not the full details):
Then you get the min and max:
Option 2. (Only if you don't have PHP 5.5 or better) The same as option 1, but to pluck the values, use
Option 3. Option 4. If you
only need a min OR max,
This does more ashleedawg 19.3k7 gold badges69 silver badges99 bronze badges answered May 1, 2011 at 3:11 RudieRudie 50.2k41 gold badges129 silver badges172 bronze badges 0
answered May 1, 2011 at 3:09 Crayon ViolentCrayon Violent 31.6k5 gold badges53 silver badges78 bronze badges For the people using PHP 5.5+ this can be done a lot easier with array_column. Not need for those ugly array_maps anymore. How to get a max value:
How to get the min value
answered Jan 24, 2014 at 11:04 RJD22RJD22 10.1k3 gold badges26 silver badges35 bronze badges It is interesting to note that both the solutions above use extra storage in form of arrays (first one two of them and second one uses one array) and then you find min and max using "extra storage" array. While that may be acceptable in real programming world (who gives a two bit about "extra" storage?) it would have got you a "C" in programming 101. The problem of finding min and max can easily be solved with just two extra memory slots
answered May 1, 2011 at 6:08 rjha94rjha94 4,1923 gold badges28 silver badges36 bronze badges 1 How about without using predefined
functions like
Bilal 2,28021 silver badges42 bronze badges answered Sep 10, 2016 at 14:39
answered Jan 10, 2013 at 6:44
answered Nov 11, 2013 at 19:33 HanifeogluHanifeoglu 492 silver badges11 bronze badges
answered Jan 21, 2016 at 2:29 PanditPandit 7481 gold badge6 silver badges22 bronze badges 1 print fast five maximum and minimum number from array without use of sorting array in php :-
answered Jan 17, 2017 at 6:04 1 Not the answer you're looking for? Browse other questions tagged php arrays extract or ask your own question.How do you find the maximum and minimum value of an array in PHP?$min = min($numbers); $max = max($numbers); Option 2. (Only if you don't have PHP 5.5 or better) The same as option 1, but to pluck the values, use array_map : $numbers = array_map(function($details) { return $details['Weight']; }, $array); How do you find the minimum and maximum value in PHP?Approach 2 (Using Library Functions) : We use library functions to find minimum and maximum.. Max():max() returns the parameter value considered “highest” according to standard comparisons. ... . Min():min() returns the parameter value considered “lowest” according to standard comparisons.. How do I find the smallest number in an array in PHP?php function get_min_value($my_array){ $n = count($my_array); $min_val = $my_array[0]; for ($i = 1; $i < $n; $i++) if ($min_val > $my_array[$i]) $min_val = $my_array[$i]; return $min_val; } $my_array = array(56, 78, 91, 44, 0, 11); print_r("The lowest value of the array is "); echo(get_min_value($my_array)); echo("\n" ... How do you find the max value in an array?To get the index of the max value in an array:. Get the max value in the array, using the Math. max() method.. Call the indexOf() method on the array, passing it the max value.. The indexOf method returns the index of the first occurrence of the value in the array or -1 if the value is not found.. |