I'm adding an object's property into an array
using a foreach
statement but I think the code can be made more readable by using a high order function. I'm thinking maybe array_map
might work but I haven't found how to.
$deviceCollection = [];
foreach ($android as $token) {
$deviceCollection[] = $this->pushNotification->Device($token->token, ['badge' => 1]);
}
Can this be done with array_map
or am I looking at the wrong function?
Can be done, e.g. via
$afn = Closure::bind(
function($token) {
$this->pushNotification->Device($token->token, ['badge' => 1]);
},
$this
);
$deviceCollection = array_map($afn, $android);
if $afn
is used in only one place, I'd say readability is in the eye of the beholder....