动态创建 PHP 数组

Create PHP Array Dynamically

我想创建一个具有以下结果的 PHP 数组。数组值将被动态传递。

array(
    __( 'Bedroom', "my-text-domain" ) => 'Bedroom', 
    __( 'Luxury', "my-text-domain" ) => 'Luxury', 
    __( 'Modern', "my-text-domain" ) => 'Modern', 
)

我尝试了 arraypushkey => value 对。但是没有得到预期的输出。

array_push:

array_push($categories_dropdown, "__( '" . $cat_name . "',  'my-text-domain'  ) => '" . $cat_name . "'");

数组推送结果,

Array
(
    [0] => __( 'Uncategorized',  'my-text-domain'  ) => 'Uncategorized'
    [1] => __( 'Bedroom',  'my-text-domain'  ) => 'Bedroom'
    [2] => __( 'Luxury',  'my-text-domain'  ) => 'Luxury'
    [3] => __( 'Modern',  'my-text-domain'  ) => 'Modern'
    [4] => __( 'Office',  'my-text-domain'  ) => 'Office'
    [5] => __( 'Reception',  'my-text-domain'  ) => 'Reception'
    [6] => __( 'Vintage',  'my-text-domain'  ) => 'Vintage'
)

键 => 值对:

$categories_dropdown["__( '".$cat_name."',  'my-text-domain'  )"] = $cat_name;

key => 值对结果,

Array
(
    [__( 'Uncategorized',  'my-text-domain'  )] => Uncategorized
    [__( 'Bedroom',  'my-text-domain'  )] => Bedroom
    [__( 'Luxury',  'my-text-domain'  )] => Luxury
    [__( 'Modern',  'my-text-domain'  )] => Modern
    [__( 'Office',  'my-text-domain'  )] => Office
    [__( 'Reception',  'my-text-domain'  )] => Reception
    [__( 'Vintage',  'my-text-domain'  )] => Vintage
)

如何在 PHP 中动态生成上述模式数组?

"__( '".$cat_name."', 'my-text-domain' )"是一个字符串,需要函数执行的结果,所以去掉引号,调用__()函数:

$categories_dropdown[__($cat_name, 'my-text-domain')] = $cat_name;