0

I want to create a PHP array with the following result. The array values will be passed dynamically.

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

I tried both arraypush and key => value pair. But the expected output wasn't obtained.

array_push:

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

Array push results,

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

key => value pair :

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

key => value pair results,

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
)

How can I result the above mentioned patterned array dynamically in PHP?

Ramesh
  • 2,297
  • 2
  • 20
  • 42
  • `__()` is a function, why do you enclose it in quotes? – u_mulder Mar 05 '20 at 09:20
  • The array is required to create a custom element in wpBakery plugin and, it's the array pattern that the parameter accepts sir. When hard coding the values it works fine, now I want the values dynamically assigned – Ramesh Mar 05 '20 at 09:23
  • 3
    So why not `$categories_dropdown[__( $cat_name, 'my-text-domain' )] = $cat_name;`? – u_mulder Mar 05 '20 at 09:24
  • @u_mulder it result the following sir, `Array ( [Uncategorized] => Uncategorized [Bedroom] => Bedroom [Luxury] => Luxury [Modern] => Modern [Office] => Office [Reception] => Reception [Vintage] => Vintage )`. It replaces the ` __( 'Bedroom', "my-text-domain" )` with `[Bedroom]` likewise – Ramesh Mar 05 '20 at 09:31
  • 1
    Yes, the function `__()` is called. And what do you expect? – u_mulder Mar 05 '20 at 09:33
  • I expect the array value like `__( 'Bedroom', "my-text-domain" ) => 'Bedroom'` , does both result the same? – Ramesh Mar 05 '20 at 09:47
  • 1
    `'__( 'Bedroom', "my-text-domain" )'` is a __string__. Why do you need this string instead of result of caliing `__()`? – u_mulder Mar 05 '20 at 09:53
  • @u_mulder You were right. I only considered the structure of the array. But the final outcome worked perfectly. `$categories_dropdown[__( $cat_name, 'my-text-domain' )] = $cat_name;` This lined worked functionally. Make it as the answer. – Ramesh Mar 05 '20 at 10:08

1 Answers1

2

"__( '".$cat_name."', 'my-text-domain' )" is a string, and you need result of function execution, so remove quotes and call the __() function:

$categories_dropdown[__($cat_name, 'my-text-domain')] = $cat_name;
u_mulder
  • 54,101
  • 5
  • 48
  • 64