473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 2040
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
if (is_array($_POST)) { foreach ($this->getAssocSectionsObjArray($key, $dbAP) as $obj) { print_r($obj); print_r(" in array? "); print_r(in_array($obj, $result)); print_r("<P>"); if...
1
by: jerrygarciuh | last post by:
Hello, I am using this very slightly modified function found here: http://us2.php.net/mysql_fetch_object to make mySQL rows into objects of type $classname: // This takes db result rows and...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
3
by: Rich | last post by:
Hello, I am converting an app from VB6 to VB.Net and have encountered the following problem. I have the following loop which retrieves objects from a collection of objects. Dim entry As...
4
by: IanONet | last post by:
I had a need for a two dimentional array. Looking for this solution, I ran accross a statement than all Javascipt arrays were arrays of objects. So I created a function prototype, at least thats...
35
by: Frederick Gotham | last post by:
(Before I begin, please don't suggest to me to use "std::vector" rather than actual arrays.) I understand that an object can have resources (e.g. dynamically allocated memory), and so we have to...
7
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id -...
1
by: =?Utf-8?B?UiBD?= | last post by:
hi, i have an array of 30 adgroup objects, called arrAdGroup. i am using a loop to traverse through the array to set the adgroup.id property to a value (which is different every time). the code...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.