代码之家  ›  专栏  ›  技术社区  ›  Manos Dilaverakis

是否可以将数组作为命令行参数传递给PHP脚本?

  •  20
  • Manos Dilaverakis  · 技术社区  · 15 年前

    我正在维护一个PHP库,负责获取和存储传入的数据(POST、GET、命令行参数等)。我刚刚修复了一个bug,它不允许从POST和GET获取数组变量,我想知道这是否也适用于处理命令行的部分。

    能否将数组作为命令行参数传递给PHP?

    9 回复  |  直到 15 年前
        1
  •  18
  •   dev-null-dweller    15 年前

    直接不是,在命令行中传递的所有参数都是字符串,但您可以使用查询字符串作为一个参数来传递所有具有名称的变量:

    php myscript.php a[]=1&a[]=2.2&a[b]=c
    
    <?php
    parse_str($argv[1]);
    var_dump($a);
    ?>
    
    /*
    array(3) {
      [0]=> string(1) "1"
      [1]=> string(3) "2.2"
      ["b"]=>string(1) "c"
    }
    */
    
        2
  •  11
  •   Dal Hundal    15 年前

    严格地说,没有。但是您可以传递序列化的 serialize() unserialize() 或者使用json)数组作为参数,只要脚本将其反序列化。

    像这样的

    php MyScript.php "{'colors':{'red','blue','yellow'},'fruits':{'apple','pear','banana'}}"
    

    我不认为这是理想不过,我建议你想一个不同的方式来解决任何问题,你试图解决。

        3
  •  9
  •   Victor    13 年前

    shell_exec('php myScript.php '.escapeshellarg(serialize($myArray)));
    

    $myArray = unserialize($argv[1]);
    
        4
  •  4
  •   pondix    11 年前

    下面的代码块将以一组逗号分隔的值的形式传递数组:

    <?php
      $i_array = explode(',',$argv[1]);
      var_dump($i_array);
    ?>
    

    输出:

    php ./array_play.php 1,2,3
    
    array(3) {
      [0]=>
      string(1) "1"
      [1]=>
      string(1) "2"
      [2]=>
      string(1) "3"
    }
    
        5
  •  4
  •   Rashmi Jain    7 年前

    base64_encode(json_encode($arr));
    

    同时发送和解码它,同时在其他脚本中接收。

    json_decode(base64_decode($argv[1]));
    

    这也将解决json接收时键和值之间没有引号的问题。因为没有引号,它被认为是 错误的json 你将无法解码。

        6
  •  1
  •   Salman Arshad    15 年前

        7
  •  1
  •   Paul    13 年前

    按照以下说明进行操作后,您可以拨打如下电话:

    要使其工作,您需要在调用脚本之前执行代码。我在linux上使用bash shell,在我的.bashrc文件中设置了命令行界面,使php ini标志auto\u prepend\u文件加载我的命令行引导文件(该文件应该在php\u include\u路径的某个地方找到):

    alias phpcl='php -d auto_prepend_file="system/bootstrap/command_line.php"'
    

    <?php
    // Parse the variables given to a command line script as Query Strings of JSON.
    // Variables can be passed as separate arguments or as part of a query string:
    //    _GET='{ "key1": "val1", "key2": "val2" }' foo='"bar"'
    // OR
    //    _GET='{ "key1": "val1", "key2": "val2" }'\&foo='"bar"' 
    if ($argc > 1)
    {
       $parsedArgs = array(); 
    
       for ($i = 1; $i < $argc; $i++)
       {
          parse_str($argv[$i], $parsedArgs[$i]);
       }
    
       foreach ($parsedArgs as $arg)
       {
          foreach ($arg as $key => $val)
          {
             // Set the global variable of name $key to the json decoded value.
             $$key = json_decode($val, true);
          }
       }
    
       unset($parsedArgs);
    }
    ?>
    

    它遍历所有传递的参数,并使用 variable variables (注意$$)。手册页上确实说变量不适用于超全局变量,但它似乎适用于我的$GET(我猜它也适用于POST)。我选择将值作为JSON传入。json_decode的返回值在出现错误时将为空,如果需要,应该对decode进行错误检查。

        8
  •  1
  •   Adam    11 年前

    某种程度上。

    如果你通过这样的考试:

    $php script.php --opt1={'element1':'value1','element2':'value2'}
    

    在opt1参数中可以得到:

    Array(
     [0] => 'element1:value1'
     [1] => 'element2:value2'
    )
    

    因此,您可以使用以下代码段转换:

    foreach($opt1 as $element){
        $element = explode(':', $element);
        $real_opt1[$element[0]] = $element[1];
    }
    

    它变成了这样:

    Array(
     [element1] => 'value1'
     [element2] => 'value2'
    )
    
        9
  •  0
  •   Mwayi    11 年前

    所以如果CLI是这样的

    php path\to\script.php param1=no+array param2[]=is+array param2[]=of+two
    

    那么读取这个的函数可以是

    function getArguments($args){
        unset($args[0]); //remove the path to script variable
        $string = implode('&',$args);
        parse_str($string, $params);
        return $params;
    }
    

    这会给你

    Array
    (
        [param1] => no array
        [param2] => Array
                 (
                     [0] => is array
                     [1] => of two
                 )
    )