stijn0713 Posted June 26, 2012 Share Posted June 26, 2012 I want to determine the pivot in a table and therefore i need to find the key of the lowest value in a certain 'row' of a two dim array. I came up with this: $laagstegetal = $tabel[$m-1][3]; for ($i = 2; $i <$n; $i++){ if ($tabel[$m-1][$i] < $laagstegetal){ $laagstegetal = $tabel[$m-1][$i]; } $indexGrootsteMC = array_search($laagstegetal, $tabel[$m-1]); echo $laagstegetal; } Here, $laagstegetal is supposed to contain the lowest value of the row $m -1. However, $tabel[$m-1][$i] doesn't overwrite the value holded by $laagstegetal but always adds it for some reason. In the end it holds like : -7500-7500-8200-10500-10500-10500-10500-10500 instead of -10500. What is wrong in my thinking and how could i fix it? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/264822-determining-pivot/ Share on other sites More sharing options...
jcanker Posted June 26, 2012 Share Posted June 26, 2012 There's not enough code here to be sure, because for starters I'm not sure why you've initialized your for loop the way you have. Why start $i at 2? what is $n? That aside, it probably doesn't have anything directly to do with the error you're seeing. Is the line $laagstegetal = $tabel[$m-1][3]; The only spot where you initalize $laagstegetal? Did you retype this code into the forum window or did you copy/paste? A cause of your issue isn't jumping out at me, but if you have retyped it rather than copy/paste, having $laagstegetal .= $tabel[$m-1][$i]; instead of: $laagstegetal = $tabel[$m-1][$i]; certainly would cause the issue you're seeing Quote Link to comment https://forums.phpfreaks.com/topic/264822-determining-pivot/#findComment-1357153 Share on other sites More sharing options...
stijn0713 Posted June 26, 2012 Author Share Posted June 26, 2012 There's not enough code here to be sure, because for starters I'm not sure why you've initialized your for loop the way you have. Why start $i at 2? what is $n? I should start with $i = 3. Because my tables are so that in row $m-1 ($m being the number of rows), column 0,1,2 are always empty so i can start with column 3. That aside, it probably doesn't have anything directly to do with the error you're seeing. Is the line $laagstegetal = $tabel[$m-1][3]; The only spot where you initalize $laagstegetal? . Yes. Did you retype this code into the forum window or did you copy/paste? A cause of your issue isn't jumping out at me, but if you have retyped it rather than copy/paste, having $laagstegetal .= $tabel[$m-1][$i]; instead of: $laagstegetal = $tabel[$m-1][$i]; . Nope that's not the problem Quote Link to comment https://forums.phpfreaks.com/topic/264822-determining-pivot/#findComment-1357156 Share on other sites More sharing options...
jcanker Posted June 26, 2012 Share Posted June 26, 2012 try thowing in an echo statement in that loop so you can see the value of each one as it passes through and how it affects $laagstegetal. Is the value being appended to it? Prepended? Is every value being attached or just some of them? A quick echo will help determine that an narrow it down Quote Link to comment https://forums.phpfreaks.com/topic/264822-determining-pivot/#findComment-1357161 Share on other sites More sharing options...
stijn0713 Posted June 26, 2012 Author Share Posted June 26, 2012 I fixed now. I just had to take the echo statement out of the for loop. My bad! thanks for the quick reply btw. Quote Link to comment https://forums.phpfreaks.com/topic/264822-determining-pivot/#findComment-1357163 Share on other sites More sharing options...
jcanker Posted June 29, 2012 Share Posted June 29, 2012 Ahh.....so it wasn't really adding the values, they were just all output to the screen in succession so it seemed like it I always bracket my echo statements with <br/> to make it easier to delineate the information it's trying to tell me Looking at the returns, at least you know it's sticking with the lowest compared value like you intended Quote Link to comment https://forums.phpfreaks.com/topic/264822-determining-pivot/#findComment-1357847 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.