Php 数组:找出不同之处!
我无法弄清楚这段 PHP 代码片段中 $arrayParams
的最终结果之间有什么区别。
该函数采用字符串 $types
和另一个名为 $params
的参数。 $params
本身可以是任何类型的单个值,也可以是任何类型的值数组。
此代码的目的是在代码中稍后绑定到 call_user_func_array()
。
最终的$types
字符串,然后以下索引将是传递的字符串的引用作为$params
。
if(is_array($params)) {
// Make a new array, first index is $types string.
$arrayParams = array($types);
// Loop over $params array and add the pointer of each index to $arrayParams.
// ??? This doesn't seem to be working ???
foreach($params as $p) {
$arrayParams[] = &$p;
}
}
else {
// This works fine here, very simple.
$arrayParams = array($types, &$params);
}
当将 $params
作为数组传递时,$array_params
的 var_dump
显示第一个之后的所有键都是指向相同值的指针 (? )
I can't figure out what the difference is between the final result of $arrayParams
in this snippet of PHP.
The function takes string $types
, and another parameter called $params
. $params
itself can be a single value of any type, or an array of values of any type.
The purpose of this code is for binding to call_user_func_array()
later on in the code.
The final $arrayParams
variable needs to be an array with index 0 as the original $types
string, and then the following indices are to be references of the string(s) passed in as $params
.
if(is_array($params)) {
// Make a new array, first index is $types string.
$arrayParams = array($types);
// Loop over $params array and add the pointer of each index to $arrayParams.
// ??? This doesn't seem to be working ???
foreach($params as $p) {
$arrayParams[] = &$p;
}
}
else {
// This works fine here, very simple.
$arrayParams = array($types, &$params);
}
The var_dump
of $array_params
, when passing $params
as an array shows all keys after the first to be a pointer to the same value (?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
foreach
对$params
数组的副本进行操作,除非您指定在迭代中使用引用。foreach
operates on a copy of the$params
-array, unless you specify to use references on the iteration.