stijn0713 Posted June 28, 2012 Share Posted June 28, 2012 I checked it manually 3x but still i can't seem to get it :S $laagsteGet = 'error'; for ($y = 0; $y < count($laagsteBbreukX); $y++){ if ($laagsteBbreukX[$y] != 'error' && $laagsteGet = 'error'){ $laagsteGet = $laagsteBbreukX[$y]; } else if ($laagsteBbreukX[$y] != 'error' && $laagsteBbreukX[$y] < $laagsteGet){ $laagsteGet = $laagsteBbreukX[$y]; } } $laagsteBbreukX[$y] is an array that contains : Array ( [0] => 94.444444444444 [1] => 26.666666666667 [2] => 156.25 [3] => error ) I want to pick the lowest value and put it in $laagsteGet, however it returns me always 156.25??? Quote Link to comment https://forums.phpfreaks.com/topic/264955-stupid-overlooking/ Share on other sites More sharing options...
xyph Posted June 28, 2012 Share Posted June 28, 2012 I'm not sure exactly what you're doing, but min($array); will return the lowest value in an array. Also, = is for assignment, == is for comparison. Quote Link to comment https://forums.phpfreaks.com/topic/264955-stupid-overlooking/#findComment-1357748 Share on other sites More sharing options...
stijn0713 Posted June 28, 2012 Author Share Posted June 28, 2012 ok, but the values can also be strings which need to be ignored Quote Link to comment https://forums.phpfreaks.com/topic/264955-stupid-overlooking/#findComment-1357763 Share on other sites More sharing options...
xyph Posted June 28, 2012 Share Posted June 28, 2012 if( !is_array($variable) ) will check if the variable is not an array. Quote Link to comment https://forums.phpfreaks.com/topic/264955-stupid-overlooking/#findComment-1357773 Share on other sites More sharing options...
ManiacDan Posted June 28, 2012 Share Posted June 28, 2012 for ($y = 0; $y < count($laagsteBbreukX); $y++){ if ($laagsteBbreukX[$y] != 'error' && $laagsteGet = 'error'){ $laagsteGet = $laagsteBbreukX[$y]; } else if ($laagsteBbreukX[$y] != 'error' && $laagsteBbreukX[$y] < $laagsteGet){ $laagsteGet = $laagsteBbreukX[$y]; } } This code says "for every item in the array, if the item is not 'error', use it." The 'else if' is never hit. That's why you're getting the last item in the list. You could also use array_filter to strip everything but the numbers by using $arr = array_filter($arr, 'is_float'); then use min(). Quote Link to comment https://forums.phpfreaks.com/topic/264955-stupid-overlooking/#findComment-1357784 Share on other sites More sharing options...
DavidAM Posted June 28, 2012 Share Posted June 28, 2012 While some good suggestions have been provided - particularly the array_filter() and min() functions, the problem with that code was pointed out by xyph in the first response. This statement: if ($laagsteBbreukX[$y] != 'error' && $laagsteGet = 'error'){ is assigning "error" to $laagsteGet instead of testing, and results in a True condition, so the if is always true (except for the last entry in the sample array). The code should probably be: if ($laagsteBbreukX[$y] != 'error' && $laagsteGet == 'error'){ Notice the second "=" in the last comparison. Quote Link to comment https://forums.phpfreaks.com/topic/264955-stupid-overlooking/#findComment-1357793 Share on other sites More sharing options...
ManiacDan Posted June 29, 2012 Share Posted June 29, 2012 While David is correct about = vs ==, I'm also right about the faulty logic, regardless of the operator used. Quote Link to comment https://forums.phpfreaks.com/topic/264955-stupid-overlooking/#findComment-1357866 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.