View Single Post
Old 05-09-2007, 16:33   #11 (permalink)
Cl1mh4224rd
Registered User
 
Join Date: Aug 2007
Posts: 2
PHP Code:
/**
 * A callback function to be used with array_walk(). Rather than having multiple
 * callbacks that share the same structure, I use "variable functions" so the
 * function to be applied to the value can be specified when array_walk() is
 * called. This function supports multidimensional arrays.
 *
 * Example: array_walk( $_POST, 'rml_aw_callback', 'htmlentities' );
 *
 * This will apply htmlentities() to EVERY value in the $_POST array.
 *
 * NOTE: If the callback is passed the name of a function that doesn't exist, it
 * will trigger an error and immediately return. This will happen for each
 * top-level value (i.e. subarrays will be passed over).
 */
function rml_aw_callback( &$value$key$function )
{
    
// Just in case someone decides to try and pass something other than a
    // string as the function name.
    
$function = (string)$function;

    
// Make sure the function exists before trying to call it. If it doesn't,
    // trigger an error and return immediately.
    
if( !function_exists$function ) ) {
        
trigger_error"rml_aw_callback(): Function \"{$function}\" does not exist"E_USER_ERROR );
        return;
    }

    if( !empty( 
$value ) ) {
        if( 
is_array$value ) ) {
            
array_walk$value'rml_aw_callback'$function );
        } else {
            
$value $function$value );
        }
    }


If anyone was wondering, the "rml" in the function name is just my initials.

If you're also wondering why I would do this when there's the array_walk_recursive() function that does practically the same things, it's because array_walk_recursive() is PHP5 and our server is only running PHP4 at the moment.
  Reply With Quote