Search code examples
phparraysloops

Declaration in loop only preserving last encountered value


All, I've got the following code:

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 ),
        )
    ),

This works fine but I'd basically like to add options to my options array dynamically. I tried to do something like this:

$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,
    ),

This gives me the error:

Parse error: syntax error, unexpected T_DOUBLE_ARROW

Is there a way to go about doing something like this?? Thanks


Solution

  • Try this:

    $menu_options = array();
    foreach ( $menus as $menu ) :
        $menu_options[] = array( 'single_twitter' => __( 'Show Single Tweet', TB_GETTEXT_DOMAIN));
    endforeach;
    

    Edit: Per the comment, you could try something like:

    foreach ( $menus as $menu ) :
        $menu_options[] = array( $menu->menu_id => __( $menu->name, TB_GETTEXT_DOMAIN));
    endforeach;