是否可以将数组作为命令行参数传递给PHP脚本? [英] Is it possible to pass an array as a command line argument to a PHP script?

查看:132
本文介绍了是否可以将数组作为命令行参数传递给PHP脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



可以将数组作为命令行参数传递给PHP吗?

解决方案

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

  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
}
* /
pre>

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 allow it to fetch array variables from POST and GET and I'm wondering whether this is also applicable to the part that deals with the command line.

Can you pass an array as a command line argument to PHP?

解决方案

Directly not, all arguments passed in command line are strings, but you can use query string as one argument to pass all variables with their names:

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"
}
*/

这篇关于是否可以将数组作为命令行参数传递给PHP脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆