1

This is as equally a drupal question as a PHP question, I assume.

I have the following print_r($node) array

stdClass Object
(
    [vid] => 4
...
    [field_imgleft] => Array
            (
            [und] => Array
                (
                    [0] => Array
                        (
                            [value] => defaultimgleft
                            [format] => 
                            [safe_value] => defaultimgleft
                        )
                )
        )
)

field_imgleft is a field in the content type of the node. Since there will only be one value, [0] is the max for that array. I'm trying to return the value of [value] to a variable, but I am having no such luck with node-> methods or node[...] and so on.

1
  • Please post the code you tried. Commented Jun 19, 2012 at 21:36

2 Answers 2

5

There's a built in API function to extract field values from an entity: field_get_items().

You can use it like so:

$items = field_get_items('node', $node, 'field_imgleft');

$first_item = array_shift($items);

$value = $first_item['value'];

This method is recommended over accessing the array directly as it takes care of the field translation for you...no need to worry about using 'und' any more.

Sign up to request clarification or add additional context in comments.

Comments

2

I would suggest using the Devel module so you can tap into the dpm() function as an improved print_r();

dpm( $node->field_imgleft['und'][0]['value'] );

1 Comment

As far as I know Drupal questions are stil fine here, as long as the crux of the question is related to programming.

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.