Jump to content

[SOLVED] Objects vs Arrays


Jonob

Recommended Posts

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

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++
}

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.