Jump to content

Variable not being assigned


Go to solution Solved by pintee,

Recommended Posts

This has been driving me crazy. When I put:

for($i = 0; $i < sizeOf($clones); $i++) {
        $clones[$i]->attackGroup = $_POST['type'][$i];
}

$clones[2]->attackGroup does not get assigned. But when I put:

for($i = 0; $i < sizeOf($clones); $i++) {
    if($i == 2) {    
        $clones[$i]->attackGroup = $_POST['type'][$i];
    }

}

It does.....How is this possible????

Link to comment
https://forums.phpfreaks.com/topic/280322-variable-not-being-assigned/
Share on other sites

There is something missing. Both cases should work.

 

Are there any error messages?

 

Does this code work?

foreach($clones as $key => &$value) {
        $value->attackGroup = $_POST['type'][$key];
}

Without seeing more code, it's nearly imposible to figure out what's wrong.

Let me verify a few things:

 

$clones is an array, correct?

$clones[2] is an object of some type, correct?

Are $clones[0] and $clones[1] the same type of object?

 

If $clones[0] and/or $clones[1] are not the same type of object as $clones[2] then trying to assign something to $clones[0 or 1]->attackGroup will throw an error and stop the script, and it will never get to $clones[2].

 

We difinetely need more information.

$clones is an array.

 

$clones[2] is an object.

 

$clones[0] and $clones[1] are the same type of object.

 

Interestingly....when I spell out the for loop explicitly:

$clones[0]->attackGroup = $_POST['type'][0];
$clones[1]->attackGroup = $_POST['type'][1];
$clones[2]->attackGroup = $_POST['type'][2];
$clones[3]->attackGroup = $_POST['type'][3];

it does not assign....

 

but when i just have:

$clones[2]->attackGroup = $_POST['type'][2];

it does.......

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.