简体   繁体   English

如何在php中传递数组中的多个function参数?

[英]How to pass multiple function parameters in an array in php?

I want to pass multiple parameters to a function in an array.我想将多个参数传递给数组中的 function。 The parameters themselves are arrays of strings.参数本身是 arrays 个字符串。

I have a function called new_action() , which I call in the following way:我有一个名为new_action()的 function,我通过以下方式调用它:

new_action(array('actions' => array('ACTION1','ACTION2'), 'tables' => array('table1','table2')));

and that function looks like this: function 看起来像这样:

public function new_action($params=array()) {
  $tables = array();
  $actions = array();

  if(count($params)) {
    foreach($params as $k => $v) {
      $tables  = ($k == 'tables')  ? $v : array();
      $actions = ($k == 'actions') ? $v : array();
    }

    // Do useful stuff with the variables here ...
    var_dump($tables);
    var_dump($actions);

    // ======== ACTUAL OUTPUT: ========
    // array(0) { }
    // array(0) { }

    // ======== EXPECTED OUTPUT: ========
    // array(2) { [0]=> string(6) "table1" [1]=> string(6) "table2" }
    // array(2) { [0]=> string(7) "ACTION1" [1]=> string(7) "ACTION2" }
  } 
}

As you can see, the value of $v inside the loop (the array of strings) is never being copied to the variable $tables or $actions outside the loop.如您所见,循环内$v的值(字符串数组)永远不会被复制到循环外的变量$tables$actions

I think this is most likely a scope problem but looking at answers to other questions similar to this hasn't elucidated the solution for me.我认为这很可能是 scope 问题,但查看与此类似的其他问题的答案并没有为我阐明解决方案。

How do I refactor the code so that I can access those arrays stored in $v for each $k outside the foreach loop?我如何重构代码,以便我可以访问存储在$v中的那些foreach for each 循环外的每个$k

Your code is overwriting what was previously set because you are setting the values to array() when the key doesn't match tables or actions .您的代码正在覆盖之前设置的内容,因为当键与tablesactions不匹配时,您将值设置为array() So when $k == 'tables' , $actions gets set back to an empty array.所以当$k == 'tables'时, $actions被设置回一个空数组。

Instead of the ternary operator, basically an if/else , refactor to just an if if you want to keep the loop:如果你想保持循环,而不是三元运算符,基本上是if/else ,重构为if

foreach($params as $k => $v) {
        if ($k == 'tables') {
            $tables = $v;
        }
        if ($k == 'actions') {
            $actions = $v;
        }

    }

Or it can be condensed a bit by explicity checking for those keys at the start instead of looping:或者它可以通过在开始时显式检查这些键而不是循环来压缩一点:

function new_action($params=array()) {

    $tables = isset($params['tables']) ? $params['tables'] : array();

    $actions = isset($params['actions']) ? $params['actions'] : array();

    // Do useful stuff with the variables here ...
    var_dump($tables);
    var_dump($actions);

}

You actually don't need the foreach to get that output:您实际上不需要 foreach 来获取 output:

public function new_action($params = array()) {
  $tables = array_key_exists('tables', $params) ? $params['tables'] : [];
  $actions = array_key_exists('actions', $params) ? $params['actions'] : [];

  // Do useful stuff with the variables here ...
  var_dump($tables); // Outputs => array(2) { [0]=> string(6) "table1" [1]=> string(6) "table2" }
  var_dump($actions); // Outputs => array(2) { [0]=> string(7) "ACTION1" [1]=> string(7) "ACTION2" }
  } 
}

The values are empty, because with the foreach you are overwriting existing values.这些值是空的,因为使用 foreach 会覆盖现有值。

You can even write it more simple and use array shorthand syntax with brackets for better readability.您甚至可以将其编写得更简单,并使用带括号的数组速记语法来提高可读性。

The null coalesce operator is syntactic sugar to avoid writing isset() and also is ternary. null 合并运算符是避免编写isset()的语法糖,也是三元运算符。 If it is set, it will return its contents, otherwise set to an empty array.如果已设置,它将返回其内容,否则设置为空数组。

new_action(['actions' => ['ACTION1', 'ACTION2'], 'tables' => ['table1', 'table2']]);

function new_action($p = array()): void {
    $tables  = $p['tables']  ?? [];
    $actions = $p['actions'] ?? [];

    var_dump($tables);
    var_dump($actions);
}

array(2) { [0] => string(6) "table1" [1] => string(6) "table2" } array(2) { [0] => string(6) "table1" [1] => string(6) "table2" }
array(2) { [0] => string(7) "ACTION1" [1] => string(7) "ACTION2" }数组(2){ [0] => 字符串(7)“ACTION1”[1] => 字符串(7)“ACTION2”}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM