代码之家  ›  专栏  ›  技术社区  ›  Edward Tanguay

如何将$params数组作为参数传递给函数?

  •  0
  • Edward Tanguay  · 技术社区  · 14 年前

    如何将函数中各种类型参数的数组转换为实际参数?

    $params = array( 'the title2', 'the footer2', 10);
    displayTest(implode(',', $params)); //how???
    displayTest(convert_to_parameters($params)); //how???
    
    function displayTest($title, $footer, $numberOfItems = 0) {
        $r = '';
        $r .= '<div>'.$title.'</div>';
        for($index = 0; $index < $numberOfItems; $index++) {
            $r .= '<div>the text</div>';
        }
        $r .= '<div>'.$footer.'</div>';
        return $r;
    }
    
    1 回复  |  直到 14 年前
        1
  •  2
  •   Justin Spahr-Summers    14 年前

    你可以用 call_user_func_array() 为此:

     call_user_func_array('displayTest', $params);
    
    推荐文章