Внутри цикла foreach я назначаю массив переменной.
Поскольку этот массив находится в цикле, он будет выводить более одного раза.
Поскольку он будет выводиться более одного раза, мне нужно, чтобы конец массива имел запятую, чтобы он не разбивал массив при каждом возврате и экземпляре массива.
Есть ли способ сделать это?
— Я нашел способы в Интернете, но они только показали, как это сделать со строками в циклах foreach, чтобы добавить или удалить запятую в конце последнего цикла foreach.
Мой код ниже, чтобы объяснить.
// ----------------------------------------------------------------------------------------------------
// 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;
}
}
Многочисленные проблемы с кодом. И тут нет ничего общего с настоящими запятыми. А точнее структура данных функции вывода.
В этом куске кода:
<?php
$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()
))
),
);
tabs
объявляется только как массив с 1 значением. Там нет никакого способа, вы можете положить несколько значений в tabs
как это. Это должно быть изменено так:
<?php
$options[] =
array(
'title' => 'Layout Settings',
'name' => 'layout-settings',
'icon' => 'fa fa-cog',
'fields' =>
array(
// ----------------------------------------------------------------------
// TAB - Layout Settings
// ----------------------------------------------------------------------
array('type' => 'tabbed', 'id' => 'layout_settings', 'tabs' => return_post_type_layout_settings())
),
);
Затем вам нужно изменить свою функцию, чтобы она возвращала правильный формат массива:
function return_post_type_layout_settings() {
$public_post_type = get_post_types(
array(
'_builtin' => TRUE,
'public' => TRUE
)
);
sort($public_post_type, SORT_NATURAL);
$layout_options = array(); // initialize $layout_options as array
foreach($public_post_type as $post_type) {
// append each options into $layout_options
$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'),
))
));
}
// return after $layout_options is finished
return $layout_options;
}
Является ли целью иметь $layout_options
быть массивом из нескольких ["title"=>"My Layout", "fields"=>[..]]
объекты? Если так, я бы сделал что-то вроде этого:
function return_post_type_layout_settings() {
$public_post_type = get_post_types(
array(
'_builtin' => TRUE,
'public' => TRUE
)
);
sort($public_post_type, SORT_NATURAL);
$layout_options = array();
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'),
))
))
;
}
return $layout_options;
}