Jump to content

stupid overlooking


stijn0713

Recommended Posts

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???

Link to comment
https://forums.phpfreaks.com/topic/264955-stupid-overlooking/
Share on other sites

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().

Link to comment
https://forums.phpfreaks.com/topic/264955-stupid-overlooking/#findComment-1357784
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/264955-stupid-overlooking/#findComment-1357793
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.