Skip to content
Advertisement

Tag: arrays

PHP and adding same value to several array keys

I’m using PHP. I would like to add same value to several array keys. This works: Anyway, I’m looking for a shorter way. So, I have tried this: This did not work at all. Any ideas how the job could be done with less code? Answer the obvious and most readable variant is: another option is:

PHP Spread Syntax in Array Declaration

PHP supports the spread syntax for variadic functions. In JavaScript, you can use the spread syntax to do this: However, trying to do this in PHP: Results in this error: Parse error: syntax error, unexpected ‘…’ (T_ELLIPSIS), expecting ‘]’ Is using the spread syntax this way not allowed in PHP? If so, is there an equally-as-elegant way to achieve the

Advertisement