Php 数组:找出不同之处!

发布于 2024-10-07 07:07:39 字数 912 浏览 3 评论 0原文

我无法弄清楚这段 PHP 代码片段中 $arrayParams 的最终结果之间有什么区别。

该函数采用字符串 $types 和另一个名为 $params 的参数。 $params 本身可以是任何类型的单个值,也可以是任何类型的值数组。

此代码的目的是在代码中稍后绑定到 call_user_func_array()

最终的$arrayParams 变量需要是一个索引为 0 的数组作为原始 $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_paramsvar_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲凉≈ 2024-10-14 07:07:39
// ...
foreach($params as &$p) {
    $arrayParams[] = &$p;
}
unset($p);
// ...

foreach$params 数组的副本进行操作,除非您指定在迭代中使用引用。

// ...
foreach($params as &$p) {
    $arrayParams[] = &$p;
}
unset($p);
// ...

foreach operates on a copy of the $params-array, unless you specify to use references on the iteration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文