0

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?

1
  • 2
    Have you tried: $first_name = $profile['main']->field_first_name['und'][0]['value'];? Commented Feb 3, 2013 at 17:33

2 Answers 2

4

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'];
Sign up to request clarification or add additional context in comments.

2 Comments

Out of curiosity, why didn't the 'field_first_name' need to be in quotes, as it was in the array?
because it's not an array key, it's an object property. php.net/manual/en/oop5.intro.php
1

This should fix your error and return the expected value:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.