Saruman Saruman - 2 months ago 6
PHP Question

GET request array from .htaccess to php file

I have a small problem.
Lets say we have the following lines in our .htaccess file:

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]


When I get the URL in index.php, its basically:


/something/somethingelse/yes


If i were to send an array as the 3th argument, how do I cast it to a PHP array:


/api/firstArgument/second[]=1,second[]=,second[]=3/third


This will get routed to my function which might look like this:

public function api($first, array $second, $third) {}


I don't want to call it like

/api/?firstArgument=something&econd[]=1&second[]=2&second[]=3&third=something

So how do I extract the array from the URL if I know that the 2 argument should be an array?

Is there a built-in function for this or should i perhaps rewrite my routing? :)

Answer

Php uses the ampersand to separate query paramteres.

Your also missing the "?" Which indicates you have parameters.

Try:

/something/somethingelse?id[]=1&id[]=2&id[]=3/hello

With the new edit, you can use parse_str to convert your second argument to an array. This would be done in your router.

Parses str as if it were the query
string passed via a URL and sets 
variables in the current scope.

http://php.net/manual/en/function.parse-str.php