Skip to content

Instantly share code, notes, and snippets.

@przor3n
Created January 4, 2016 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save przor3n/0268724892b81bac1cbd to your computer and use it in GitHub Desktop.
Save przor3n/0268724892b81bac1cbd to your computer and use it in GitHub Desktop.
yield and iterator_to_array as a dynamic generator
private function getSearchTypeList()
{
foreach ($this->choices as $choice) {
yield $choice => 'type.' . $choice;
}
}
//then
iterator_to_array($this->getSearchTypeList())
//returns:
// [ 'a' => 'type.a', 'b' => 'type.b' ... ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment