user1015214
user1015214

Reputation: 3091

retriving data from a php object inside an array

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?

Upvotes: 0

Views: 47

Answers (2)

BenM
BenM

Reputation: 53246

This should fix your error and return the expected value:

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

Upvotes: 1

Danilo Kobold
Danilo Kobold

Reputation: 2581

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'];

Upvotes: 4

Related Questions