Skip to content

Instantly share code, notes, and snippets.

@pramendra
Created November 10, 2017 02:29
Show Gist options
  • Save pramendra/85aee559a095b9d9f04d8762c3826d29 to your computer and use it in GitHub Desktop.
Save pramendra/85aee559a095b9d9f04d8762c3826d29 to your computer and use it in GitHub Desktop.
<?php
$picker = function ($item) {
return $item;
};
function arrayImplodeIntoString(array $list, $picker, string $separator = ',') :string
{
$first_element = $list[0]??null;
if ($first_element) {
$remaining_list = array_slice($list, 1);
return array_reduce($remaining_list, function ($carry, $item) {
return $carry . ','. $item;
}, $first_element);
}
return null;
}
$list = [1, 2, 3];
echo arrayImplodeIntoString($list, $picker);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment