I'm not sure if that title is phrased exactly right but my that is how my IDE (PHPStorm) describes it.
Using the splat operator, I want to take an array that looks like this:
array(
array(
'label' => 'Some label',
'value' => 'some-value'
)
)
And transform it into one that looks like this:
array(
'some-value' => 'Some label'
)
I'm new to using the "splat operator" in PHP and I was wondering why this code is considered invalid:
$key = ...array_map( function( $el ) {
return array(
$el['value'] => $el['label']
);
}, array(
array(
'label' => 'Some label',
'value' => 'some-value'
)
) );
But this code works fine, achieves my goal, but the array_merge
is completely pointless:
$key = array_merge( array(), ...array_map( function( $el ) {
return array(
$el['value'] => $el['label']
);
}, array(
array(
'label' => 'Some label',
'value' => 'some-value'
)
) ) );
Wrapping it in parentheses doesn't seem to help. Is this not what the splat operator was at least partially intended to do? It seems like a shame to have to pass it through a superfluous function to make it work.
Is there another PHP function I'm unaware of that does the same thing without the redundancy?
based ont this: https://wiki.php.net/rfc/spread_operator_for_array the spread operator is coming in 7.4.
we only have argument unpacking: https://wiki.php.net/rfc/argument_unpacking
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.