好的,我正在尝试将参数放入一个被调用的函数中:
$parameters['function']($parameters['params']);
以下是我需要放入的函数和参数:
$parameters['function'] = 'test_error';
$parameters['params'] = array(0 => $txt['sometext'], 1 => 'critical', 2 => true);
test_error函数有3个参数:
以下是我得到的输出: 这是一个测试错误.ArraycriticalArray1
我知道这个功能很完美,但它只给了我从它返回的第一个参数。我在$parameters['params']
??
编辑:这是功能:
function test_error($type = 'error', $error_type = 'general', $echo = true)
{
global $txt;
// Build an array of all possible types.
$valid_types = array(
'not_installed' => $type == 'not_installed' ? 1 : 0,
'not_allowed' => $type == 'not_allowed' ? 1 : 0,
'no_language' => $type == 'no_language' ? 1 : 0,
'query_error' => $type == 'query_error' ? 1 : 0,
'empty' => $type == 'empty' ? 1 : 0,
'error' => $type == 'error' ? 1 : 0,
);
$error_html = $error_type == 'critical' ? array('<p class="error">', '</p>') : array('', '');
$error_string = !empty($valid_types[$type]) ? $txt['dp_module_' . $type] : $type;
// Should it be echoed?
if ($echo)
echo implode($error_string, $error_html);
// Don't need this anymore!
unset($valid_types);
}
答案 0 :(得分:2)
你可能想要call_user_func_array()
。作为第二个参数传递的数组的每个项目将用作函数参数,例如:
call_user_func_array( $parameters['function'], $parameters['params'] );
答案 1 :(得分:0)
见下文。
http://www.php.net/manual/en/function.call-user-func-array.php
<?php
function foobar($arg, $arg2) {
echo __FUNCTION__, " got $arg and $arg2\n";
}
class foo {
function bar($arg, $arg2) {
echo __METHOD__, " got $arg and $arg2\n";
}
}
// Call the foobar() function with 2 arguments
call_user_func_array("foobar", array("one", "two"));
// Call the $foo->bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));
?>