object(Profile)#151 (20) { ["field_first_name"]=> array(1) { ["und"]=> array(1) { [0]" /> object(Profile)#151 (20) { ["field_first_name"]=> array(1) { ["und"]=> array(1) { [0]" /> object(Profile)#151 (20) { ["field_first_name"]=> array(1) { ["und"]=> array(1) { [0]"/>

retriving data from a php object inside an array

50 Views Asked by At

I have an object contained inside an array

array(1) { 
["main"]=> object(Profile)#151 (20) 
{ 
["field_first_name"]=> array(1) { ["und"]=> array(1) { [0]=> array(3) { ["value"]=> string(6) "Fred" ["format"]=> NULL ["safe_value"]=> string(6) "Fred" } } } 
} 

}

I am trying to get the value "Fred" from this array. I thought I could do this

$first_name= $profile['main']->['field_first_name']['und'][0]['value'];

but it didn't work. It actually gave me an error

Parse error: syntax error, unexpected '[', expecting T_STRING or T_VARIABLE or '{' or '$'

What am I doing wrong?

2

There are 2 best solutions below

2
Danilo Kobold On BEST ANSWER

field_first_name is a property of $profile['main'] wich is an object.

$profile['main']->field_first_name;

And the code you added in your example would be like this.

$first_name= $profile['main']->field_first_name['und'][0]['value'];
0
BenM On

This should fix your error and return the expected value:

$first_name = $profile['main']->field_first_name['und'][0]['value'];