Jump to content

Determining pivot


stijn0713

Recommended Posts

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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

 

Link to comment
Share on other sites

Ahh.....so it wasn't really adding the values, they were just all output to the screen in succession so it seemed like it :shrug:

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 :)

 

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.