Php 在数组中使用foreach,而数组本身也在数组中

Php 在数组中使用foreach,而数组本身也在数组中,php,arrays,wordpress,foreach,admin,Php,Arrays,Wordpress,Foreach,Admin,我使用startbox管理面板在wordpress管理面板上工作,我想在一个数组中列出所有带有slug和name的类别,但我似乎无法使其工作,下面是我的代码 $args = array( 'type' => 'post', 'child_of' => 0, 'parent' => '', 'orderby' => 'name', '

我使用startbox管理面板在wordpress管理面板上工作,我想在一个数组中列出所有带有slug和name的类别,但我似乎无法使其工作,下面是我的代码

$args = array(
'type'                     => 'post',
'child_of'                 => 0,
'parent'                   => '',
'orderby'                  => 'name',
'order'                    => 'ASC',
'hide_empty'               => 1,
'hierarchical'             => 1,
'exclude'                  => '',
'include'                  => '',
'number'                   => '',
'taxonomy'                 => 'category',
'pad_counts'               => false 

);

$args = array(
   'orderby' => 'name',
   'order' => 'ASC'
);

$categories = get_categories($args);

// This much above code is given by wordpress to get all categories, mine starts below

class sb_slider_settings extends sb_settings {

function sb_slider_settings() {
    $this->slug = 'sb_slider_settings';
    $this->options = array(
        'on_off_news' => array(
                'type'      => 'select',
                'default'   => 'true',
                'label'     => __( 'Enable breaking news', 'startbox' ),
                'options'   => array(
                    'false' => __( 'No', 'startbox' ),
                    'true'  => __( 'Yes', 'startbox' ),
                )
            ),              
        'ticker_text' => array(
                'type'      => 'select',
                'default'   => 'true',
                'class'     => 'ticker_text',                   
                'label'     => __( 'Extract posts from', 'startbox' ),
                'options'   => array(
                         foreach($categories as $category) {
                             $category->slug => __( $category->name , 'startbox' ) ;
                         }
                )
            )

        );
        parent::__construct();
}
}
它显示的错误


Parse error:syntax error,意外的'foreach'(T_foreach),预期'”

您不能在数组中执行代码。数组中的值是固定的


要执行您想要执行的操作,您必须创建一个函数并在数组中调用该函数。

但是,您可以使用array\u map对数组的每个元素调用相同的函数:

就你而言

'options'   => array_map('callback', $categories),
你的回电呢

function callback($category) {
    return __( $category->name , 'startbox' );
);

数组内的
foreach
构造?你是认真的吗?
foreach
内部
array()
完全无效。您应该描述
$categories
是什么,以及您希望
options
最终是什么。是的回调函数返回一个值,但数组映射对每个项使用此函数,并返回一个数组。使用:print_r(数组映射('callback',$categories));返回空数组:数组([1]=>[2]=>[3]=>[4]=>[5]=>[6]=>[7]=>[8]=>[10]=>)