全部, 我有以下代码:
array(
'id' => 'tweet_slider',
'name' => __( 'Slide', TB_GETTEXT_DOMAIN ),
'desc' => __( 'Select the icon you\'d like shown before the Tweet.', TB_GETTEXT_DOMAIN ),
'type' => 'select',
'std' => 'twitter_display_option',
'options' => array(
'single_twitter' => __( 'Show Single Tweet', TB_GETTEXT_DOMAIN ),
'slider_twitter' => __( 'Show Twitter Slider', TB_GETTEXT_DOMAIN ),
)
),
这很好但我基本上想要动态地为我的选项数组添加选项。我试着做这样的事情:
$menus = wp_get_nav_menus();
foreach ( $menus as $menu ) :
$menu_options = 'single_twitter' => __( 'Show Single Tweet', TB_GETTEXT_DOMAIN ),
endforeach;
$menu_options = (array)$menu_options;
array(
'id' => 'menu_options',
'name' => __( 'Enter in your Menu Options', TB_GETTEXT_DOMAIN ),
'desc' => __( 'Enter in your menu options.', TB_GETTEXT_DOMAIN ),
'std' => '',
'type' => 'select',
'options' => $menu_options,
),
这给了我错误:
解析错误:语法错误,意外T_DOUBLE_ARROW
有没有办法去做这样的事情?感谢
答案 0 :(得分:4)
试试这个:
$menu_options = array();
foreach ( $menus as $menu ) :
$menu_options[] = array( 'single_twitter' => __( 'Show Single Tweet', TB_GETTEXT_DOMAIN));
endforeach;
修改:根据评论,您可以尝试以下内容:
foreach ( $menus as $menu ) :
$menu_options[] = array( $menu->menu_id => __( $menu->name, TB_GETTEXT_DOMAIN));
endforeach;
答案 1 :(得分:1)
您收到语法错误,因为您无法在该位置使用=>
符号。您可以使用以下命令将项添加到数组中:
$menu_options['single_twitter'] = __( 'Show Single Tweet', TB_GETTEXT_DOMAIN );
或者,一般来说:
$menu_options = array();
foreach ($menus as $menu) {
$key = ...
$value = ...
$menu_options[$key] = $value;
}
使用$key
获取$value
和$menu
。在主数组中,您可以编写:
'options' => $menu_options
答案 2 :(得分:0)
从头开始构建数组的方法是
$myArray = array();
$myArray['some_key'] = some_value ;
$myArray['some_other_key'] = some_other_value ;
尝试将其应用于foreach循环中的情况。