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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.