如何在数组的数组中使用Foreach和If语句?

时间:2016-08-30 作者:Ethan O\'Sullivan

我正在使用Tareq\'s WordPress Settings API 开发带有设置页面的插件。

使用API,我显示复选框项。但是,我想使用foreachif 陈述

  • foreach: 这将为中的每个字段显示其他复选框wp_get_user_contact_methods()
  • if: 如果另一个插件被激活,这将显示一组额外的复选框,这是我现在根据自己的逻辑得出的结论:

    $settings_fields = array( // Parent array
        \'dsbl_basics\' => array( // Child array
            array( // Child\'s child array
                \'name\' => \'text_val\',
                \'label\' => __( \'Text Input\', \'dsbl\' ),
                \'type\' => \'text\',
                \'default\' => \'Title\',
                \'sanitize_callback\' => \'intval\'
            )
        ),
        \'dsbl_profile\' => array( // Child array
            array( // Child\'s child array
                \'name\' => \'name\',
                \'label\' => __( \'Name\', \'dsbl\' ),
                \'type\' => \'multicheck\',
                \'options\' => array(
                    \'first_name\' => \'First Name\',
                    \'last_name\' => \'Last Name\'
                )
            ),
            array( // Child\'s child array
                \'name\' => \'contact_info\',
                \'label\' => __( \'Contact Info\', \'dsbl\' ),
                \'type\' => \'multicheck\',
                \'options\' => array(
                    \'url\' => \'Website\',
                    foreach ( wp_get_user_contact_methods() as $value => $label ) { // Additional contact fields
                        $value => $label
                    }
                )
            ),
            if ( is_plugin_active( \'wordpress-seo/wp-seo.php\' ) ) { // If plugin exists
                array( // Child\'s child array
                    \'name\' => \'yoast_seo\',
                    \'label\' => __( \'Yoast SEO\', \'dsbl\' ),
                    \'type\' => \'multicheck\',
                    \'options\' => array(
                        \'wpseo_author_title\' => \'Title to use for Author page\',
                        \'wpseo_author_metadesc\' => \'Meta description to use for Author page\'
                    )
                ),
            }
        )
    );
    
    我知道我的语法已关闭,这给了我以下错误:

    Parse error: 语法错误,意外foreach (T\\u FOREACH),应为)

    Parse error: 语法错误,意外if (T\\u IF),应为)

    正确的方法是什么?

1 个回复
最合适的回答,由SO网友:Ethan O\'Sullivan 整理而成

根据this answer.

不能放置任何循环或控制结构if/elsearray 值或其中的任何位置array().

示例

$contact_list = [];
$contact_list[\'url\'] = \'Website\';
foreach ( wp_get_user_contact_methods() as $value => $label ) { 
    $contact_list[$value] = $label;
}
$settings_fields = array( // Parent array
    \'dsbl_basics\' => array( // Child array
        array( // Child\'s child array
            \'name\' => \'text_val\',
            \'label\' => \'Text Input\'
        )
    ),
    \'dsbl_profile\' => array( // Child array
        array( // Child\'s child array
            \'name\' => \'name\',
            \'label\' => \'dsbl\',
            \'options\' => array(
                \'first_name\' => \'First Name\',
                \'last_name\' => \'Last Name\'
            )
        ),
        array( // Child\'s child array
            \'name\' => \'contact_info\',
            \'label\' => \'Contact Info\', 
            \'options\' => $contact_list
        )
    )
);
$yoast_plugin = array( // Child\'s child array
    \'name\' => \'yoast_seo\',
    \'label\' => \'Yoast SEO\', 
    \'options\' => array(
        \'wpseo_author_title\' => \'Title to use for Author page\',
        \'wpseo_author_metadesc\' => \'Meta description to use for Author page\'
    )
);
if ( is_plugin_active( \'wordpress-seo/wp-seo.php\' ) ) {
    $settings_fields[\'dsbl_profile\'][1] = $yoast_plugin;
}