Reading a field collection I am trying to read a field collection and build an array in PHP.

The array I want to build goes like this:(it's in JS notation but you can easily imagine the equivalent in PHP)

highlights = [
{highlightTitle:A sample test title,
 highlightImage: a sample img url, 
 highlightBody: a sample text body},
{highlightTitle:A sample test title, 
 highlightImage: a sample img url,
 highlightBody: a sample text body}
]


As you can see it is just an array with objects inside, where each object represents a field collection entry. That field collection entry has 3 fields obviously, highlightTitle,highlightImage &amp; highlightBody.

Now I want to read that field collection and build the array I described above.


  The thing I care about is best practises. What is the best possible
  way to do this?


Currently I use the following code dumped in &lt;?php ?&gt; brackets on the top of my layout's tpl files: 
(I use Omega 4, and I have different layouts for different content types)

This is the code I use now:

  $highlightsArr; //this is the result array 
  $highlightsCounter = 0;
  $highlights = field_get_items('node', $node, 'field_highlights');


  foreach ($highlights as $item) {

    $fc = field_collection_field_get_entity($item);
    $highlightSnippet = $fc-&gt;field_highlight_snippet["und"][0]["value"];

    $arr = $fc-&gt;field_highlight_snippet["und"][$i]["value"];
    $innerArray = entity_load('field_collection_item', array($arr));

    $innerCounter = 0;
    foreach ($innerArray as $item) {

      $highlightsArr[$innerCounter]["sectionItemTitle"] = $item-&gt;field_highlights_title["und"][0]["value"];
      $highlightsArr[$innerCounter]["highlightsImage"] = file_create_url($item-&gt;field_highlights_image["und"][0]["uri"]);
      $highlightsArr[$innerCounter]["sectionItemPrice"] = $item-&gt;field_highlights_body["und"][0]["value"];

      $innerCounter++;
    }
  }


The issue with the above code:

The code above returns undefined variables/indexes on things that are obviously defined. When I first create a content type and want to view it's node, it returns errors. But as soon as I refresh the page the errors go away which seems rather strange to me.



In any case I would like to know best practises for doing this kind of stuff. 
Unfortunately I come from a strictly JS background (I'm a front-end dev), so Drupal and PHP is something new for me.

Many thanks in advance.
The code above returns undefined variables/indexes on things that are obviously defined.


As long as your PHP installation is not buggy, that's not possible. If the variable is defined, PHP won't tell you it isn't defined.


  as soon as I refresh the page the errors go away which seems rather strange to me.


It's not strange - messages are held in the session so if you're doing something that invokes them in a template file (when the messages for the current page have already been prepared), you should expect to see this delayed reaction. Logic should never been in a template file, always a preprocess or process function.

Best practice is really a matter of opinion, but these are the two standard options for accessing fields on any entity type, Field Collection included.


field_get_items()
EntityMetadataWrapper


Both alleviate the need to access the arrays directly, thereby decreasing the possibility of developer error.