Tried to post this to the news group before, but I'm having trouble getting
to my news server from work - hopefully I'm not posting a duplicate.
I think I'm seeing the problem I'm seeing because I'm getting a copy of a
var instead of a reference to it, but I'm not sure the best we to get around
this problem.
In my little sample script, I've got an array of objects. When I use
foreach to loop through the array and make a change to an item, it doesn't
change the object in the array, just the var that I have while I'm in the
foreach loop.
What's the right way to loop through this array if I really want to change
homer's name to marge in this example? The way it is now, I see my echo
saying that I'm changing the name, but when I do the second var_dump, it's
the same as the first var_dump.
Thanks in advance,
Jesse
<?php
class Name {
var $firstName;
var $lastName;
function Name($first, $last) {
$this->firstName = $first;
$this->lastName = $last;
}
}
$names[] = new Name("joe", "shmo");
$names[] = new Name("billy", "bob");
$names[] = new Name("homer", "simpson");
?>
<html>
<body>
<?php echo var_dump($names) ?>
<br>
<?php
foreach ($names as $name) {
if (strcmp($name->firstName, "homer") == 0) {
echo "changing homer to marge<br>";
$name->firstName = "marge";
break;
}
}
?>
<?php echo var_dump($names) ?>
<br>
</body>
</html>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php