More closures & array_walk()

One function that I had always seen and never found a good use for is array_walk().

Basically array_walk() will iterate over an array and execute a function for each item in the array. you can do some things with this, and I found a use for this combined with closures and DBTNG.

One of the many good things with DBTNG is that it is very easy to do a multi row insert, and if you are writing an array of values you can do the following

$insert = db_insert('example_table')
  ->fields('entity_type', 'entity_id', 'tid');
array_walk($tids, function ($tid, $key) use ($node, $insert) {
  $insert->values(array('entity_type' => 'node', 'entity_id' => $node->nid, 'tid' => $tid));

Yes this can be done with a foreach() but putting it inside a function means that you have a clean workspace on each interation. Also array_walk() allows you to pass a single value as a parameters, but this way you can share as many values as you need with the closure, and not mucking around with adding many variables by reference as part of an array.

Another way to create nice simple clean code.