470,815 Members | 1,103 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

Changing Object Data in an Array of Objects

Hello,

In this sample script I create an array of objects. Print out their data
with print_r().Update their data with a sub called v_set(). Print out data
showing the chnages using print_r(). I push the altered objects into a new
array and print them out again, still see the updated data. Then I iterate
over the original array again with print_r and the objects lose the updates
and have their original data.

Can anyone explain what I am doing incorrectly?

TIA,

jg

<?
for ($i = 0; $i < 1; $i++) {
$listingObj = new Listing;
$listings[] = $listingObj;
}

foreach ($listings as $aListing) {
echo "<h3>THIS IS IN SCRIPT IN ORIG ARRAY BEFORE VARS SET</h3>";
f_array($aListing);
}

foreach ($listings as $aListing) {
$aListing->v_set(
array(
'name' => 'Jane',
'address' => '321 Ohce Ln.'
)
);
// HERE IS BUG: WHY DO I NEED TO PUT IN NEW ARRAY?
echo "<h3>THIS IS IN SCRIPT IN ORIG ARRAY AFTER VARS SET</h3>";
f_array($aListing);
$newArray[] = $aListing;
}

foreach ($newArray as $aListing) {
echo "<h3>THIS IS IN NEW ARRAY</h3>";
f_array($aListing);
}

foreach ($listings as $aListing) {
echo "<h3>THIS IS IN ORIG ARRAY, APPARENTLY NOT
ALTERED/UPDATED?</h3>";
f_array($aListing);
}

class Listing {
var $changedVals = array();
var $name = 'Dick';
var $address = '123 Echo Ln.';

function v_set($data){
foreach ($data as $key => $value) {
$this->$key = $value;
$this->changedVals[] = $key;
}
}
}
function f_array($array) {
echo '<pre>';
print_r($array);
echo '</pre>';
}

?>
Jul 17 '05 #1
2 1939
jerrygarciuh wrote:
....
Can anyone explain what I am doing incorrectly? .... foreach ($listings as $aListing) {
$aListing->v_set(
array(
'name' => 'Jane',
'address' => '321 Ohce Ln.'
)
);
// HERE IS BUG: WHY DO I NEED TO PUT IN NEW ARRAY?


http://www.php.net/foreach

There's a note:

... foreach operates on a copy of the specified array ...

(snip)
So, when you do $aListing->v_set(); you're not changing the $listings
array but the copy foreach is using.

Try array_walk()
http://www.php.net/array_walk

--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #2
Thank you!!!

jg

"Pedro Graca" <he****@hotpop.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
jerrygarciuh wrote:
...
Can anyone explain what I am doing incorrectly?

...
foreach ($listings as $aListing) {
$aListing->v_set(
array(
'name' => 'Jane',
'address' => '321 Ohce Ln.'
)
);
// HERE IS BUG: WHY DO I NEED TO PUT IN NEW ARRAY?


http://www.php.net/foreach

There's a note:

... foreach operates on a copy of the specified array ...

(snip)
So, when you do $aListing->v_set(); you're not changing the $listings
array but the copy foreach is using.

Try array_walk()
http://www.php.net/array_walk

--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Phil Powell | last post: by
1 post views Thread by jerrygarciuh | last post: by
8 posts views Thread by Mike S. Nowostawsky | last post: by
4 posts views Thread by IanONet | last post: by
35 posts views Thread by Frederick Gotham | last post: by
7 posts views Thread by Steve | last post: by
1 post views Thread by =?Utf-8?B?UiBD?= | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.