Pass Array Of Function Names With Parameters As A Parameter To Function

I know it is possible to pass a single function name with its parameter(s) as a parameter using call_user_func() or [URL=“http://us.php.net/manual/en/function.call-user-func-array.php”]call_user_func_array() for a function with an array of parameters. But is there any way to pass an array of function names with their parameters to a function?

Let’s say I have 4 functions like this:

func1($param1, $param2);
func2();
func3($param3);
func4($param4, $param5, $param6);

I would like to pass all the function names with their parameters to a single function. But I cannot figure out how to do it, or if it is even possible.

I tried passing a complex array to the called function similar to this (I don’t remember exactly how I tried it):

$func_array[0][‘func’] = ‘func1’;
$func_array[0][‘param’][0] = $var1;
$func_array[0][‘param’][1] = $var2;
$func_array[1][‘func’] = ‘func2’;
$func_array[2][‘func’] = ‘func3’;
$func_array[2][‘param’][0] = $var3;
$func_array[3][‘func’] = ‘func4’;
$func_array[3][‘param’][0] = $var4;
$func_array[3][‘param’][1] = $var5;
$func_array[3][‘param’][2] = $var6;

Processing the array in the called function with foreach was a pain and I could not get it to work right. Unwinding the array with foreach, I could test the key to see if it equals ‘func’, but I couldn’t figure out what to do with the $func_array[0][‘param’] array. I couldn’t find a function to “explode” an array, either.

Anyone have any ideas or know of a better way? There has to be a way this can be done.

You could do something like this:


<?php
$func_array = array(
    array(
        'func1',
        array($var1, $var2)
    )
);

foreach ($func_array as $func)
{
    call_user_func_array($func[0], $func[1]);
}
?>

You will have to modify this a little if not all of your function calls have parameters, but this is the general direction that you should go.

Thanks, ethanp. I’ll tweak my code and give that a try.

By the way, I’m in the Twin Cities, too. How’s your web development business going?

Let me know if it works or if you need more help. Cool that you’re in the Twin Cities too!