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

5
  • 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! Commented Jan 22, 2017 at 19:37
  • 1
    @T.Todua Something like this: 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. Commented Jan 22, 2017 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 Commented Jan 22, 2017 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. Commented Jan 23, 2017 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. Commented Jan 23, 2017 at 18:55

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.