Skip to content

Instantly share code, notes, and snippets.

@ritalin
Created February 27, 2013 10:19
Show Gist options
  • Save ritalin/5046903 to your computer and use it in GitHub Desktop.
Save ritalin/5046903 to your computer and use it in GitHub Desktop.
フラットな配列にならないお・・・・
<?php
var_dump(
Ginq::from(['a' => [1,2], 'b' => [3]])
->selectMany(function ($group) { return $group; })
->toArray()
);
// 結果
// array(2) { [0]=> int(3) [1]=> int(2) }
@akanehara
Copy link

selectMany は inner のオリジナルキーを維持して平坦化するので、反復子としては

[0 => 1, 1 => 2, 0 => 3]

となり、これを toArray() した結果

[0 => 3, 1 => 2]

となったわけですね。ここで ->renum() が使えます。

var_dump(
  Ginq::from(['a' => [1,2], 'b' => [3]])
  ->selectMany(function ($group) { return $group; })
  ->renum()
  ->toArray()
);
[0 => 1, 1 => 2, 3 => 3]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment