Skip to content

Instantly share code, notes, and snippets.

@blainelang
Last active February 14, 2021 18:40
Show Gist options
  • Save blainelang/6382464 to your computer and use it in GitHub Desktop.
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.
/* 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);
@zahra-ove
Copy link

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