I have a field collection called field_sections
that has inside it it 2 fields:
field_section_title
(just plain text field)
field_menu_item
(field collection with 4 fields)
field_menu_item
has 4 fields
- field_item_title
- field_menu_image
- field_item_price
- field_item_description
I would like to build an array sections, and those sections are objects that contain menu_items, which are objects that contain menu_items.
I have tried something so far, but unfortunately I can only get the sections and not the nested menu_items and their values. For some reason in their place, only a number is contained instead of the actual field value.
How can I build those arrays?
ps:my apologies for the basic question, but I'm just now getting my hands dirty with PHP and this further complicates things for me.
This is what I did so far:
//prepare array
$sections;
$sectionsCounter = 0;
$node = node_load(8);
//Load sections field collection
$items = field_get_items('node', $node, 'field_section');
foreach ($items as $item) {
$sectionItems;
$sectionItemsCounter = 0;
$fc = field_collection_field_get_entity($item);
$sectionTitle = $fc->field_section_title["und"][0]["value"];
//push item into array now
$sections[$sectionsCounter]["sectionTitle"] = $sectionTitle;
$sectionsCounter++;
}
I have only managed to get the title of each section so far and push it into an array as an object with property title
UPDATE:
Well ,it seems I somehow got it to work, but I'm worried this is not the best solution. Can someone please have a look and tell me if I'm doing this the correct way?
//Load sections field collection
$sections;
$sectionsCounter = 0;
$node = node_load(8);
$items = field_get_items('node', $node, 'field_section');
foreach ($items as $item) {
$sectionItems;
$fc = field_collection_field_get_entity($item);
$sectionTitle = $fc->field_section_title["und"][0]["value"];
$sections[$sectionsCounter]["sectionTitle"] = $sectionTitle;
$itemsField = $fc->field_menu_item["und"][0]["value"];
$innerCounter = 0;
for ($i=0; $i <sizeof($fc->field_menu_item["und"]); $i++) {
$arr = $fc->field_menu_item["und"][$i]["value"];
$innerArray = entity_load('field_collection_item', array($arr));
foreach ($innerArray as $item) {
$sections[$sectionsCounter]["sectionItem"+$i] = $item;
}
$innerCounter++;
}
$sectionsCounter++;
}
I've managed to create 2 objects that each contain:
A section title An object that holds all the fields that the 'inner' field collection of the section contains