How to pass an array as a URL parameter? How to pass an array as a URL parameter? laravel laravel

How to pass an array as a URL parameter?


You can serialize the JSON:

myArray = ['aaa', 'bbb', 'ccc'];var arrStr = encodeURIComponent(JSON.stringify(myArray));$('#myLink').attr({ href: '/myLink?array=' + arrStr });

If your parsing (on the next page) is done via JavaScript too, you'll conversely use JSON.parse(). In PHP it would be json_decode().


It should not depend on the server side: By the URI standard specification, all parameters must have one name and should have one value. But there can be several parameters with the same name, which is the right way to serialize an array:

http://server/context?array=aaa&array=bbb&array=ccc&otherparameter=x

You could do it like this:

var s="";for (var i=0;i< myArray.length;i++){  s+="&myArray="+myArray[i];}var url="http://server/context?"+s;


try this

$('#myLink').attr({"href" : '/myLink?array=' + myArray.join(',')});

on server: capture and split data.