I found a very interesting article by Stephen Tweeddale from the team at Computer Minds referring to the correct way to extract a value from a Drupal array/object combination... If you use the trusty PHP function print_r(); on $node, (a la print_r($node); ), you'll see a great list of information, which is incredibly useful.
However, as Stephen points out, using the following code to extract a value is not the best way to extract that info...
$valueImTryingToGet = $node->field_name['und'][0]['safe_value'];
It should be done like:
$output = field_view_field('node', $node, 'field_name');
Or another way to a single item, as in if the value doesn't have multiples... so a thumbnail image for example
$node = node_load($nid); $field = field_get_items('node', $node, 'field_name'); $output = field_view_value('node', $node, 'field_name', $field[$delta]);
Note: you might not need the first line $node = node_load($nid); if $node is already available.
Another example if you wanted to find the name of category this $node had been put in... (lets say the category was a list of days from Mon to Sun). I could use $node->field_event_category['und'][0]['tid'], but instead I'll use:
$eventField = field_get_items('node', $node, 'field_event_category'); $eventTerm = field_view_value('node', $node, 'field_event_category', $eventField[0]); $titleImLookingFor = $eventTerm['#title'];
Another example from Stephen to change a value(s) before rendering is:
$node = node_load($nid); $image = field_get_items('node', $node, 'field_image'); $output = field_view_value('node', $node, 'field_image', $image[0], array( 'type' => 'image', 'settings' => array( 'image_style' => 'thumbnail', 'image_link' => 'content', ), ));
Check out the full article for more info...
Good work Stephen!