Skip to content

Instantly share code, notes, and snippets.

@jakzal
Last active July 28, 2021 16:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakzal/07144f06b5ab73be0fdab0a5c641ded0 to your computer and use it in GitHub Desktop.
Save jakzal/07144f06b5ab73be0fdab0a5c641ded0 to your computer and use it in GitHub Desktop.
Flat map in PHP
<?php
function flat_map(callable $callback, array $collection) {
return array_merge([], ...array_map($callback, $collection));
};
@jakzal
Copy link
Author

jakzal commented Feb 23, 2018

Test: https://3v4l.org/qQNTv

var_dump(
    array_map(function ($el) { return str_split($el); }, ["two birds", "three green peas"])
);

var_dump(
    flat_map(function ($el) { return str_split($el); }, ["two birds", "three green peas"])
);

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