Initializing shared sections of an array immediately

Perhaps this is impossible, I never saw him myself, but thought that I would ask. If this is my array,

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

Is there a way to initialize an array to set the same values ​​right away? as

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

Of course, I do not expect this syntax to work, but is there anything that gives me the same idea?

+3
source share
2 answers

The PHP manual does not describe any way to do this. BTW, you can initialize the values ​​as follows:

$myarr['red'] = $myarr['green'] = 7;
$myarr['blue'] = $myarr['pink'] = $myarr['brown'] = 18;
$myarr['cyan'] = 14;
+2
source

This is not possible, and frankly, I do not see a situation where it can be useful if duplicate values ​​are in the same array.

Would you like to give an example so that I can get it?

Distract attention:

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

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

0
source

Source: https://habr.com/ru/post/1784223/


All Articles