Is it possible to pass an array as a command line argument to a PHP script?

前端 未结 9 889
忘掉有多难
忘掉有多难 2020-12-09 16:37

I\'m maintaining a PHP library that is responsible for fetching and storing incoming data (POST, GET, command line arguments, etc). I\'ve just fixed a bug that would not all

9条回答
  •  醉酒成梦
    2020-12-09 16:44

    So if a CLI is as such

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

    Then the function thats reads this can be

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

    This would give you

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

提交回复
热议问题