3

Maybe this isn't possible, I've never seen it myself, but thought I'd ask. If this is my array,

$myarr = array(
   'red'   => 7, 
   'green' => 7,
   'blue'  => 18,
   'cyan'  => 14, 
   'pink'  => 18
   'brown' => 18
);

is there a way while initializing the array to set similar values at once? like

'red' && 'green' =>7, 
'blue' && 'pink' && 'brown' => 18,
'cyan' =>14

of course I'm not expecting this syntax to work but is there something that gets me the same idea?

2 Answers 2

2

PHP Manual does not provide description of any way to do that. BTW, you may initialize values in the following way:

$myarr['red'] = $myarr['green'] = 7;
$myarr['blue'] = $myarr['pink'] = $myarr['brown'] = 18;
$myarr['cyan'] = 14;
Sign up to request clarification or add additional context in comments.

2 Comments

Good thinking. Do I have to initialize $myarr to empty array $myarr = array(); before this then or no?
initialization with array() function is not necessary. If $myarr does not exist yet, new array will be created on the first assignment.
0

It isn't possible and, honestly, I fail to see a situation it could be useful if the repeated values are in the same array.

Would you like to provide an example, in order for me to get it?

A fun aside:

$bibi = array (
   'foo' == 'bar' => 2,
);
$bubu = array (
   'foo' && 'bar' => 2,
);

Both this syntaxes actually evaluate the expressions on the left. As in, in 2 is assigned to $bibi[0] and $bubu[1].

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.