0

There exists such:

$var1 = $var2 = "blabla";

but is there a way to set similar inside array? like:

array(
  'key1'='key2' => "blabla",
   ...................
)

p.s. I dont need outside of array functions, like array_fill_keys or etc.. I want inside-array solution (if it exists).

T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • 1
    @Machavity, why you closed topic??? Have you even read it? I am building a value paired array, dont need any outside functions, instead, i wanted an answer to my question! – T.Todua Jan 22 '17 at 19:37
  • 1
    @T.Todua Something like this: http://stackoverflow.com/a/23716817 ? Or should both keys always point to the same element? If yes you probably want to do something with some references, so both keys point to the same element. But there is no native syntax to do such a thing in PHP. – Rizier123 Jan 22 '17 at 21:21
  • @T.Todua I tried to give you something as close to what you were asking for as possible. PHP doesn't support multiple assignment within the array initialization – Machavity Jan 22 '17 at 21:58
  • @Rizier123 programatically, that is good method. However, I needed more visually easier solution, like the pseudo-code I have written, but seems nothing like that is possible. – T.Todua Jan 23 '17 at 18:54
  • @Machavity you could comment the related topic, but that is not duplicate at all, quite different it is.. I needed in-line (in-aarray) solution, if it exist or not. even "NO" is an answer. instead you just closed and redirected to different topic I wasnt interested in. however, thanks for reopening. – T.Todua Jan 23 '17 at 18:55

1 Answers1

3

You can set multiple array values of an array like this. Perhaps it even works without the first line.

$a = array();
$a['key1'] = $a['key2'] = 'blablabla';

Or initialize the desired keys using this awkward syntax:

$a = array_fill_keys(array('key1', 'key2'), 'blablabla');

Although the second one works, I wouldn't use it. Better to use a couple of characters extra or even separate lines than to write such a weird line which doesn't have much benefit apart from saving a tiny bit of code.

T.Todua
  • 53,146
  • 19
  • 236
  • 237
GolezTrol
  • 114,394
  • 18
  • 182
  • 210