mathias Posted October 4, 2007 Share Posted October 4, 2007 Okey. I want to find the smallest value in my array and then write it on the screen wit echo. I thnik that i got the 'founding part' but the number does'nt been writet out on the screen. Help! <?php function hittaminstatal($j, $lista){ $min = $j; for ($j + 1; $j < sizeof($lista); $j++) { if ($lista[$j] < $lista[$min]) { $min = $j; } } return $min; } $lista = array(0 => 8, 23, 12, 4); $lista = hittaminstatal($j, $lista); echo $min; ?> Quote Link to comment https://forums.phpfreaks.com/topic/71865-find-a-value-in-an-array/ Share on other sites More sharing options...
wildteen88 Posted October 4, 2007 Share Posted October 4, 2007 If you want to get the smallest value within an array use min. You can also use max to get the biggest number in the array. Quote Link to comment https://forums.phpfreaks.com/topic/71865-find-a-value-in-an-array/#findComment-361997 Share on other sites More sharing options...
mathias Posted October 4, 2007 Author Share Posted October 4, 2007 I know that i can use those.. But i want to solve it this way to learn a bit more.. Quote Link to comment https://forums.phpfreaks.com/topic/71865-find-a-value-in-an-array/#findComment-361999 Share on other sites More sharing options...
Psycho Posted October 4, 2007 Share Posted October 4, 2007 First off you are setting $min inside the function - it is not defined outside the function. Plus, you define $lista as an array and then you redefine it as the value returned from the function. Even still, your funtion doesn't work to return the min value. Quote Link to comment https://forums.phpfreaks.com/topic/71865-find-a-value-in-an-array/#findComment-362000 Share on other sites More sharing options...
marcus Posted October 4, 2007 Share Posted October 4, 2007 who says you can't define a variable inside a function? Quote Link to comment https://forums.phpfreaks.com/topic/71865-find-a-value-in-an-array/#findComment-362001 Share on other sites More sharing options...
wildteen88 Posted October 4, 2007 Share Posted October 4, 2007 Your code wont work as you always have to set the min number. Try: function getmin($nums) { // set min number to first number in the array $min = $nums[0]; // loop through the array foreach($nums as $num) { // check that the number is less than curret min number if($num <= $min) { // set min to current number $min = $num; } } // return minimum number return $min; } $arr = array(25, 60, 8, 1, 22); echo getmin($arr); Quote Link to comment https://forums.phpfreaks.com/topic/71865-find-a-value-in-an-array/#findComment-362002 Share on other sites More sharing options...
roopurt18 Posted October 4, 2007 Share Posted October 4, 2007 who says you can't define a variable inside a function? mjdamato didn't say you couldn't. He said the variable defined in the function isn't visible outside it. Plus, you define $lista as an array and then you redefine it as the value returned from the function. I do that frequently when I have to perform a sequence of conversions on the single value, so its perfectly valid to do so. However, in this case it is a mistake (most likely) because the function is returning the index of the smallest value but without the array to use as a reference, the index isn't worth much. @ the OP <?php function hittaminstatal($lista){ $min = FALSE; if(!is_array($lista) || !count($lista)){ return $min; } foreach($lista as $key => $val){ if($min === FALSE){ $min = $key; }else if($val < $lista[$min]){ $min = $key; } } return $min; } $lista = array(8, 23, 12, 4); $min = hittaminstatal($lista); // This $min is not the same as that in the function, google 'variable scope' echo $min . ", " . $lista[$min]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/71865-find-a-value-in-an-array/#findComment-362003 Share on other sites More sharing options...
BlueSkyIS Posted October 4, 2007 Share Posted October 4, 2007 note: over the last 10 years or so i have learned the most about PHP by trying to make things easier and finding ways to simplify things. trying to learn how to do things in a more difficult way seems to me to be counterproductive. instead of learning something new or useful, you seem to be going backwards, trying to make things difficult for the sake of "learning more." min and max are written; no need to learn how they do what they do. i can't imagine what's going to happen when you start to reverse-engineer the GD and curl functions... my 2 cents. Quote Link to comment https://forums.phpfreaks.com/topic/71865-find-a-value-in-an-array/#findComment-362013 Share on other sites More sharing options...
roopurt18 Posted October 4, 2007 Share Posted October 4, 2007 Learning how things work under the hood simply opens up your options. If you have two different functions that perform the same task but perform it differently and you understand the differences, you can better decide which one to call based on your current situation. Likewise, when you are faced with a "special case" for which there is no built in function, understanding how others solved similar, more simple, problems can help you solve your own. Lastly, writing your own functions to duplicate built in methods is a good way to learn about algorithm analysis and code efficiency. There should come a time when one is familiar enough with such concepts that they use the built in methods and tools where available though. Quote Link to comment https://forums.phpfreaks.com/topic/71865-find-a-value-in-an-array/#findComment-362031 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.