Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve keys? #19

Open
jenky opened this issue Apr 30, 2023 · 1 comment
Open

Preserve keys? #19

jenky opened this issue Apr 30, 2023 · 1 comment
Labels
question Further information is requested

Comments

@jenky
Copy link

jenky commented Apr 30, 2023

Pipeline::fromIterable(['a' => 1, 'b' => 2, 'c' => 3])
    ->map(fn (int $n) => $n * 2)
    ->toArray();

gives the following result

^ array:3 [
  0 => 2
  1 => 4
  2 => 6
]

However, I expect the result should be

^ array:3 [
  'a' => 2
  'b' => 4
  'c' => 6
]

I see toArray method use \iterator_to_array($this, false) which set $preserve_keys to false. But using \iterator_to_array($pipeline) also gives the same result

@kelunik
Copy link
Member

kelunik commented Apr 30, 2023

Keys aren't supported in pipelines, if you need them, you can use an array like [$key, $value] .

@kelunik kelunik added the question Further information is requested label Apr 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Development

No branches or pull requests

2 participants