Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我想将参数发送到数组中的函数_Php_Arrays_Function - Fatal编程技术网

Php 我想将参数发送到数组中的函数

Php 我想将参数发送到数组中的函数,php,arrays,function,Php,Arrays,Function,我正在尝试使用以下代码: $arr = [ function ($a) { return $a + $a; }, function ($b) { return $b * $b; }, function ($c,$cc) { return $c % $cc - $c; }, function ($d) { return $d + 4 / $d; } ]; 如何将参数传递给此函数? 我已经学习了无参数函数的这个语句。例如: 函数(){echo'so

我正在尝试使用以下代码:

$arr = [
  function ($a) {
    return $a + $a;
},
  function ($b) {
    return $b * $b;
},
  function ($c,$cc) {
    return $c % $cc - $c;
},
  function ($d) {
    return $d + 4 / $d;
 }
];
如何将参数传递给此函数? 我已经学习了无参数函数的这个语句。例如: 函数(){echo'somethings';}这意味着数组中的此函数没有传入参数

$test = rand (0,1);
echo $myarr[$test]();
另外


我可以用上述代码段中的其他语句更改(rand)语句吗?

您可以执行以下操作:

您的阵列:

$arr = [
    function ($a) {
        return $a + $a;
    },
    function ($b) {
        return $b * $b;
    },
    function ($c,$cc) {
        return $c % $cc - $c;
    },
    function ($d) {
        return $d + 4 / $d;
    }
];
你的兰特

$test = rand(0,1);
----注----

您可以将rand()更改为任何其他语句,只要它是$arr数组的现有索引。为了知道$test value是否是$arr数组中存在的索引,可以执行以下操作

if (isset($arr[$test])) {}
----尾注---

调用函数

$returnedValue = call_user_func($arr[$test], 10);
echo $returnedValue;

您可以执行以下操作:

您的阵列:

$arr = [
    function ($a) {
        return $a + $a;
    },
    function ($b) {
        return $b * $b;
    },
    function ($c,$cc) {
        return $c % $cc - $c;
    },
    function ($d) {
        return $d + 4 / $d;
    }
];
你的兰特

$test = rand(0,1);
----注----

您可以将rand()更改为任何其他语句,只要它是$arr数组的现有索引。为了知道$test value是否是$arr数组中存在的索引,可以执行以下操作

if (isset($arr[$test])) {}
----尾注---

调用函数

$returnedValue = call_user_func($arr[$test], 10);
echo $returnedValue;
我可以用其他方式调用上面的函数(在数组中)吗?(我的意思是使用$test变量。不使用变量)。我可以用其他方式调用上面的函数(在数组中)吗?(我的意思是使用$test变量。不使用变量)。