Jump to content

Variable not being assigned


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.......

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.