Jonob Posted June 5, 2009 Share Posted June 5, 2009 I am trying to work with getting values in a php object, but stumbling at the final hurdle. The following works 100% and it creates my TestVO object perfectly. $result = new TestVO(); foreach ($trans as $value) { $result->description = $value->desc; $result->memo = $value->note; } Note, however, that I am looping through $trans, as I expect it to have many rows of data, so I need to increment the $result object accordingly. So, my code changes as follows: $i=0; $result = new TestVO(); foreach ($trans as $value) { $result[$i]->description = $value->desc; $result[$i]->memo = $value->note; $i++ } But I get the following error message: Cannot use object of type TestVO as array I'm not sure why I cant increment the object with [$i] each time....must be missing something really simple here. Thanks for any help that you can give. Link to comment https://forums.phpfreaks.com/topic/161086-solved-objects-vs-arrays/ Share on other sites More sharing options...
gevans Posted June 5, 2009 Share Posted June 5, 2009 It's an object, not an array. If you need a new instance of the object foreach of the values you need to be doning something like this; <?php $i = 0; foreach ($trans as $value) { $result[$i] = new TestVO(); $result[$i]->description = $value->desc; $result[$i]->memo = $value->note; $i++ } Link to comment https://forums.phpfreaks.com/topic/161086-solved-objects-vs-arrays/#findComment-850069 Share on other sites More sharing options...
Jonob Posted June 5, 2009 Author Share Posted June 5, 2009 Awesome, thanks again gevans! Link to comment https://forums.phpfreaks.com/topic/161086-solved-objects-vs-arrays/#findComment-850084 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.