Skip to content

Conversation

@iluuu1994
Copy link
Member

Closes GH-18581

Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was also my first thought, but now playing with it I don't understand how the following makes sense:

<?php
function g() {
    yield '1' => 'one';
    yield '0' => 'zero';
}

function test(...$z) {
    var_dump($z);
}

test(...['1'=>'one','0'=>'zero']);
test(...g());

Yields:

array(2) {
  [0]=>
  string(3) "one"
  [1]=>
  string(4) "zero"
}
array(2) {
  [0]=>
  string(3) "one"
  [1]=>
  string(4) "zero"
}

So in both calls, the item with key '1' gets assigned to array index 0...
I see it is consistent between arrays and generators though, I just don't understand how this would make sense.
This question came to me after seeing the test which generates keys 100 until 102 but ends up with lower array indices

@iluuu1994
Copy link
Member Author

Not that I disagree that the behavior is weird, but array unpacking is likely factors more common than iterator unpacking, so IMO it makes more sense to adjust the less common case, at least without RFC or discussion. For example, changing how ints are interpreted could have subtle breaks, e.g. when using asort() on an array that is later unpacked, where previously the keys were entirely discarded.

@iluuu1994 iluuu1994 closed this in 23ec35b Jul 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Argument unpacking (spread operator) doesn't normalise keys from an iterator

2 participants