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?
You need to figure out some way of encoding your array as a string. Then you can pass this string to PHP CLI as a command line argument and later decode that string.
Strictly speaking, no. However you could pass a serialized (either using PHP's
serialize()
andunserialize()
or using json) array as an argument, so long as the script deserializes it.something like
I dont think this is ideal however, I'd suggest you think of a different way of tackling whatever problem you're trying to address.
So if a CLI is as such
Then the function thats reads this can be
This would give you
Sort of.
If you pass something like this:
You get this in the opt1 argument:
so you can convert that using this snippet:
which turns it into this:
As it was said you can use serialize to pass arrays and other data to command line.
And in myScript.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: