Page 1 of 1

undefined offset?!? :@@! [SOLVED]

Posted: Tue Nov 29, 2011 5:09 pm
by Ark
Hello beutiful minds of Indie-resource I got a problem with an array telling me it's not defined but i'm defining it ('at least that's what I think...')

the strange thing is that i'm getting 9 errors and the array has 10 values...
Undefined offset: 2 in C:\wamp\www\Conquer\newengine.php on line 758

thats the same for Undefined offset: 2-10

here's the part of the script

Code: Select all

        for($i=1;$i<=10;$i++)
        {
            $taraname='u1'.$i.'name';
            $surv1[$i]=$$taraname?$unitarray1[$$taraname][1]:false;
    
            $taraname='u2'.$i.'name';
            $surv2[$i]=$$taraname?$unitarray2[$$taraname][1]:false;
    
        ////////////////////////  LOSSES
    
            $taraname='u1'.$i.'name';
            $lost1[$i]=$$taraname?$u1firstqty[$i]-$surv1[$i]:false;     /// L 758    
            if($lost1[$i]<0)
            {$lost1[$i]=0;}
            
            $taraname='u2'.$i.'name';
            $lost2[$i]=$$taraname?$u2firstqty[$i]-$surv2[$i]:false;
            if($lost2[$i]<0)
            {$lost2[$i]=0;}
        }// for(10)
 
yet another strange thing happens is that the script only marks me the error when all the values of the array are 0 ...

Code: Select all

print_r($lost1); 

Code: Select all

Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 )
But when at least there's one value that is greater than zero, there's no error and the script works fine.

Code: Select all

Array ([1] => 13 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 )
anyone got an idea why there's an undefined offset in the array?? :|

Re: undefined offset?!? :@@!

Posted: Tue Nov 29, 2011 8:02 pm
by Jackolantern
I personally cannot stand variable variables. They are confusing, lead to many errors, and are just plain silly. I highly suggest to rethink the logic on that entire block to get the variable variables out. That is the source of your problems with the offsets.

Re: undefined offset?!? :@@!

Posted: Tue Nov 29, 2011 9:19 pm
by Ark
yea you're right, but believe me in this case variable variables simplifies me alot! Yes I will have to review the whole logic as you said. But it's pretty strange I get the error sometimes and sometimes not :S

Re: undefined offset?!? :@@!

Posted: Tue Nov 29, 2011 9:40 pm
by Jackolantern
It may help you to write the code now, but I guarantee you that you will not understand it in the slighest 6 months or so from now lol. I am already struggling to understand what you are doing by the first couple of lines, and I understand variable variables lol.

Re: undefined offset?!? :@@!

Posted: Wed Nov 30, 2011 6:36 am
by Winawer
Where are you defining $u1firstqty[$i]?

Re: undefined offset?!? :@@!

Posted: Wed Nov 30, 2011 1:33 pm
by Ark
Winawer i'm defining it in the begining of the function...

Code: Select all

    for($i=1;$i<=10;$i++)
    {
        $varname='u1'.$i.'qty';
        if($$varname)
        {
            $u1firstqty[$i]=$$varname;
        }
    }
 

Re: undefined offset?!? :@@!

Posted: Wed Nov 30, 2011 1:39 pm
by Ark
ooooohh!!! That's the problem, i'm not setting it when it's false.

Damn Winawer you're a genius :)
it should be...

Code: Select all

    for($i=1;$i<=10;$i++)
    {
        $varname='u1'.$i.'qty';
        if($$varname)
        {
            $u1firstqty[$i]=$$varname;
        }
        else
        {$u1firstqty[$i]=false;}
    }