silow
silow

Reputation: 4306

Initializing array shared values at once

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?

Upvotes: 3

Views: 187

Answers (2)

cbrandolino
cbrandolino

Reputation: 5883

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

Upvotes: 0

Kel
Kel

Reputation: 7780

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;

Upvotes: 2

Related Questions