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

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

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

Post 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?? :|
Last edited by Ark on Wed Nov 30, 2011 1:40 pm, edited 1 time in total.
Orgullo Catracho
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: undefined offset?!? :@@!

Post 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.
The indelible lord of tl;dr
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: undefined offset?!? :@@!

Post 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
Orgullo Catracho
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: undefined offset?!? :@@!

Post 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.
The indelible lord of tl;dr
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: undefined offset?!? :@@!

Post by Winawer »

Where are you defining $u1firstqty[$i]?
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: undefined offset?!? :@@!

Post 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;
        }
    }
 
Orgullo Catracho
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: undefined offset?!? :@@!

Post 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;}
    }
 
Orgullo Catracho
Post Reply

Return to “Advanced Help and Support”