Last active
October 31, 2024 16:57
-
-
Save blainelang/6382464 to your computer and use it in GitHub Desktop.
Using an anonymous function with array_walk and passing in a variable so that it's in scope for the function to use.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Objective: Loop over an array of Drupal user objects and build a string of email addresses for the $to variable. | |
* At first there was an issue because the variable $to_email_array was not being accessible to the anonymous function | |
* and then I saw answer2 to a similar question: | |
* > http://stackoverflow.com/questions/11420520/php-variables-in-anonymous-functions | |
*/ | |
$to_email_array = array(); | |
array_walk($users_group, function(&$user, &$uid) use (&$to_email_array) { | |
$to_email_array[] = $user->mail; | |
}); | |
$to = implode(',', $to_email_array); |
Thank you. Is it possible to define $to_email_array
as a multidimensional array and use it in the array_walk
function?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks it works