By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

Accessing stdClass object vars

P: n/a
Hi,

I have the following object, I need to extract the SysMessage
property.

stdClass Object ( [UnknownConsignment] =stdClass Object
( [SysMessage] =Your consignment could not be found on our
system ) )

I could do this:
$object->UnknownConsignment->SysMessage

However, depending on the condition, the "UnknownConsignment" property
might be called something else.
Therefore, I need a solution, something like an array:
$object->[0]->SysMessage

Thanks in advance!

Mar 1 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
get_object_vars will actually give you an array of properties an object have

Hendri

JamesG wrote:
Hi,

I have the following object, I need to extract the SysMessage
property.

stdClass Object ( [UnknownConsignment] =stdClass Object
( [SysMessage] =Your consignment could not be found on our
system ) )

I could do this:
$object->UnknownConsignment->SysMessage

However, depending on the condition, the "UnknownConsignment" property
might be called something else.
Therefore, I need a solution, something like an array:
$object->[0]->SysMessage

Thanks in advance!
Mar 1 '07 #2

P: n/a
On Mar 1, 1:58 am, Hendri Kurniawan <ask...@email.comwrote:
get_object_vars will actually give you an array of properties an object have

Hendri

JamesG wrote:
Hi,
I have the following object, I need to extract the SysMessage
property.
stdClass Object ( [UnknownConsignment] =stdClass Object
( [SysMessage] =Your consignment could not be found on our
system ) )
I could do this:
$object->UnknownConsignment->SysMessage
However, depending on the condition, the "UnknownConsignment" property
might be called something else.
Therefore, I need a solution, something like an array:
$object->[0]->SysMessage
Thanks in advance!

Thanks for the tip.
I'm trying to use this, but it's not working how I like.

The array is now as follows:
Array ( [UnknownConsignment] =stdClass Object ( [SysMessage] =Your
consignment could not be found on our system ) )

So you would assume by using: $array[0] would access the object, and:
$array[0]->SysMessage contains the message I want.
This does not work! I know it's something silly, so any further help
is appreciated!

Thanks again

Mar 1 '07 #3

P: n/a
JamesG wrote:
On Mar 1, 1:58 am, Hendri Kurniawan <ask...@email.comwrote:
>get_object_vars will actually give you an array of properties an object have

Hendri

JamesG wrote:
>>Hi,
I have the following object, I need to extract the SysMessage
property.
stdClass Object ( [UnknownConsignment] =stdClass Object
( [SysMessage] =Your consignment could not be found on our
system ) )
I could do this:
$object->UnknownConsignment->SysMessage
However, depending on the condition, the "UnknownConsignment" property
might be called something else.
Therefore, I need a solution, something like an array:
$object->[0]->SysMessage
Thanks in advance!


Thanks for the tip.
I'm trying to use this, but it's not working how I like.

The array is now as follows:
Array ( [UnknownConsignment] =stdClass Object ( [SysMessage] =Your
consignment could not be found on our system ) )

So you would assume by using: $array[0] would access the object, and:
$array[0]->SysMessage contains the message I want.
This does not work! I know it's something silly, so any further help
is appreciated!

Thanks again
Hi James,

It doesn't work because it returns associated array.
What you have to do is get the name of that keys using array_keys.

$variables = get_object_vars($object);
$keys = array_keys($variables);
print $variables[$key[0]]->SysMessage;

Hendri Kurniawan
Mar 1 '07 #4

P: n/a
On Mar 1, 3:37 am, Hendri Kurniawan <ask...@email.comwrote:
JamesG wrote:
On Mar 1, 1:58 am, Hendri Kurniawan <ask...@email.comwrote:
get_object_vars will actually give you an array of properties an object have
Hendri
JamesG wrote:
Hi,
I have the following object, I need to extract the SysMessage
property.
stdClass Object ( [UnknownConsignment] =stdClass Object
( [SysMessage] =Your consignment could not be found on our
system ) )
I could do this:
$object->UnknownConsignment->SysMessage
However, depending on the condition, the "UnknownConsignment" property
might be called something else.
Therefore, I need a solution, something like an array:
$object->[0]->SysMessage
Thanks in advance!
Thanks for the tip.
I'm trying to use this, but it's not working how I like.
The array is now as follows:
Array ( [UnknownConsignment] =stdClass Object ( [SysMessage] =Your
consignment could not be found on our system ) )
So you would assume by using: $array[0] would access the object, and:
$array[0]->SysMessage contains the message I want.
This does not work! I know it's something silly, so any further help
is appreciated!
Thanks again

Hi James,

It doesn't work because it returns associated array.
What you have to do is get the name of that keys using array_keys.

$variables = get_object_vars($object);
$keys = array_keys($variables);
print $variables[$key[0]]->SysMessage;

Hendri Kurniawan
Thanks Hendri, works perfectly.

Mar 1 '07 #5

P: n/a
My pleasure
Mar 1 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.