1

I have been Googling for hours but I can't make heads nor tails out of it. I have a multidimensional associative array like this:

$mArray = array(
   array("m" => "0"),
   array("m" => "1"),
   array("m" => "1")
   );

I would like to create the array with PHP GET request:

mywebsite.com/file.php?.......what do I put here?.....
0

1 Answer 1

4

You need to use array access notation like this

mywebsite.com/file.php?item[0][m]=0&item[1][m]=1&item[2][m]=1

It would be simple to use nested loops to build this string - don't forget to URL encode the values

$query_string = '';
foreach($mArray as $key => $array) {
    foreach($array as $k => $v) {
        $query_string .= 'item[' . urlencode($key) . '][' . urlencode($k) . ']=' . urlencode($v) . '&';
    }
}
$query_string = substr($query_string, 0, 1); // trim extra &

If you needed to handle arrays of arbitrary dimensions, you could obviously modify this into a function which could be called recursively to get to as many levels as possible.

Sign up to request clarification or add additional context in comments.

17 Comments

Thanks, I cant seem to get it to work...Shouldn't $mArray be somewhere in the URL?
@Youss I just put item as the parameter name as you didn't specify what the name was you wanted the parameter passed as. You can simply substitute item for whatever parameter name you choose.
The whole thing depends on 'getting' the URL first, how do I do that with GET?
@Youss So I thought you wanted to take the values in $mArray and pass to another page via $_GET, so you needed to build the query string. No matter the logic is the same. If you are trying to populate $mArray from a GET request you just need to have the query string formatted as I have shown.
@Yous So now you should know both the format required in the query string to pass arrays (of any number of dimensions as you just keeping add new brackets to add dimensions). And you should know how to create such a query string from an existing PHP array. So hopefully you should have what you need.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.