【发布时间】:2020-12-27 02:36:40
【问题描述】:
假设有一个带有一些参数的函数,并且我有一个关联数组(或具有公共属性的简单对象 - 这几乎相同,因为我总是可以使用类型转换 (object)$array),其键对应于函数参数名称,其值对应于函数调用参数。我如何调用它并将它们传递到那里?
<?php
function f($b, $a) { echo "$a$b"; }
// notice that the order of args may differ.
$args = ['a' => 1, 'b' => 2];
call_user_func_array('f', $args); // expected output: 12 ; actual output: 21
f($args); // expected output: 12 ; actual output: ↓
// Fatal error: Uncaught ArgumentCountError:
// Too few arguments to function f(), 1 passed
【问题讨论】:
-
你应该展示你正在尝试做的代码示例
-
@JohnConde 我很乐意,但问题是我不知道如何在 php 中做到这一点 ????。我已经添加了一个示例 - 虽然不工作
标签: php parameter-passing associative-array function-call