Some times we find ourselves with Field Collection that have other Field Collection as childs and we need to add data to those child fields programatically. Luckily this is something that can be done, but we just need to know the correct syntaxis to do it, and that is not always obvious.
So let's go directly to the issue and lets propose this structure:
The Entity API provides classes and methods / functions that make CRUD for entities much, much easier and less prone to errors and bugs.
The first example show the creation of a field collection entity that will be attached to a node that has a field collection field already defined for it through the Manage Fields tab of the node type creation / modification UI. The example shows creating the field collection entity (using the entity_create function of the Entity API, attaching it to a node, and using the Entity API then to set values of fields within the field collection.
<?php
// Code that would be here but is omitted loads a node called $my_node
// Nodes of the type that is $my_node have a field of the field collection
// type called field_text_files, which in turn is defined as having two
// fields called field_source_txt_filename and field_source_txt_content
// Begin by using the entity_create function to create a new entity of
// type field_collection_item. The second parameter to the function
// provides "an array of values to set, keyed by property name".
// In our example, the field in my_node that holds the field collection is