Inside a foreach loop I am assigning an array to a variable.
Because this array is within a loop it will output more than once.
Because it will output more than once I need the end of the array to have a comma so it doesn't break the array for each time it returns and instance of the array.
Is there a way to do this? - I found ways online but they only showed how to do this with strings in foreach loops, to either add or remove the comma at the end of the last foreach loop.
My code is below to explain.
// ----------------------------------------------------------------------------------------------------
// Start our framework config arrays
// ----------------------------------------------------------------------------------------------------
$options = array();
// ----------------------------------------------------------------------
// MENU - Layout Settings
// ----------------------------------------------------------------------
$options[] =
array(
'title' => 'Layout Settings',
'name' => 'layout-settings',
'icon' => 'fa fa-cog',
'fields' =>
array(
// ----------------------------------------------------------------------
// TAB - Layout Settings
// ----------------------------------------------------------------------
array('type' => 'tabbed', 'id' => 'layout_settings', 'tabs' => array(
return_post_type_layout_settings()
))
),
);
// Our return array function
function return_post_type_layout_settings() {
$public_post_type = get_post_types(
array(
'_builtin' => TRUE,
'public' => TRUE
)
);
sort($public_post_type, SORT_NATURAL);
foreach($public_post_type as $post_type) {
$layout_options =
array('title' => ucwords($post_type) . ' Layout', 'fields' => array(
array('id' => $post_type, 'type' => 'grid', 'span' => '6-12', 'fields' => array(
// ----------------------------------------------------------------------
// FIELD - Header Settings Panel
// ----------------------------------------------------------------------
array('message' => 'Enable ' . ucwords($post_type). ' Header Settings?', 'video' => 'QAEjuDpIaE4', 'type' => 'title_with_help'),
array('id' => $post_type . '_enable_header', 'type' => 'switcher'),
))
)) // << I need to comma to post at the end of this array
// because in my array above this will output more than once
;
return $layout_options;
}
}
returnstatement in foreach loop will not work the way you expected. You need to cascade your results to a single variable output within your foreach and return by the end. (or use Generator Syntax).