1

Is there a way to represent/pass a multidimensional array like

array( array('foo' => 'bar') , array('language' => 'php'), ...);

into a GET string?

For simple arrays such as

array(1,2,3,4)

I can represent it like this

?ids=1,2,3,4

But I don't know how to represent multidimensional arrays

0

4 Answers 4

3

You use the same square bracket notation as when submitting form post data, eg

?array[][foo]=bar&array[][language]=php

$array = $_GET['array'];
Sign up to request clarification or add additional context in comments.

Comments

2

There's several ways you could do it.

One way is to serialize() it and then unserialize() it on the page that reads it.

Another way is to json_encode() it and then json_decode() it on the page that reads it.

Or, in keeping with the CSV-style it sounded like you prefer, you could just delimit the next layer of the array with a different delimiter, ex.

array(
    0 => array(1, 2, 3),
    1 => array(4, 5, 6),
)

becomes

?array=1|2|3,4|5|6

Finally, PHP will also parse arrays in the querystring like:

?array[0][0]=1&array[0][1]=2&array[0][1]=3&array[1][0]=4&array[1][1]=5&array[1][1]=6

Comments

0

I don't know of any standard way to do this. You can roll your own by using a space character as a row delimiter:

array( array(1, 2, 3, 4), array(5, 6, 7) );

becomes

?ids=1,2,3,4+5,6,7

Comments

0

You can use this method to prevent any disambiguation:

?ids=1|2|3|4||23|34|45

its easy to implement and read.

at the reciever's end, you just need to explode the GET parameter back to an array, eg:

$par=explode($_GET[ids],"||");

or

$par=explode($_GET[ids],"|");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.