How to pass arrays as url parameter

0 votes

What is the best way that I can pass an array as a url parameter? I was thinking if this is possible:

$aValues = array();

$url = 'http://www.example.com?aParam='.$aValues;

Ive read examples, but I find it messy:

$url = 'http://www.example.com?aParam[]=value1&aParam[]=value2&aParam[]=value3';
Sep 1, 2020 in PHP by kartik
• 37,520 points
6,818 views

1 answer to this question.

0 votes

Hello @kartik,

There is a very simple solution: http_build_query(). It takes your query parameters as an associative array:

$data = array(
    1,
    4,
    'a' => 'b',
    'c' => 'd'
);
$query = http_build_query(array('aParam' => $data));

will return

string(63) "aParam%5B0%5D=1&aParam%5B1%5D=4&aParam%5Ba%5D=b&aParam%5Bc%5D=d"

http_build_query() handles all the necessary escaping for you (%5B => [ and %5D => ]), so this string is equal to aParam[0]=1&aParam[1]=4&aParam[a]=b&aParam[c]=d.

Hope it helps!!

Thank you!!

answered Sep 1, 2020 by Niroj
• 82,880 points

Related Questions In PHP

0 votes
0 answers

How to pass a PHP variable using the URL

I need to pass some PHP variables ...READ MORE

Jun 19, 2022 in PHP by narikkadan
• 63,680 points
6,107 views
0 votes
1 answer

How to validate E-mail and URL of Php form?

hey, The code below shows a simple way ...READ MORE

answered Feb 13, 2020 in PHP by manish
2,341 views
0 votes
1 answer

How to merge two arrays while keeping keys instead of reindexing in php?

Hello, Considering that you have $replaced = array('1' => ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,880 points
2,970 views
0 votes
1 answer

How to pass an array within a query string?

Hello, Submitting multi-value form fields, i.e. submitting arrays ...READ MORE

answered Apr 8, 2020 in PHP by Niroj
• 82,880 points
12,351 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
22,960 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,969 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,900 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
44,399 views
0 votes
1 answer

How to GET URL parameter in PHP?

Hello @kartik, $_GET is not a function or language ...READ MORE

answered Sep 17, 2020 in PHP by Niroj
• 82,880 points
3,876 views
0 votes
1 answer

How to pass multiple variables to another page in url?

Hello @kartik, Use the ampersand & to glue variables together: $url ...READ MORE

answered Nov 23, 2020 in PHP by Niroj
• 82,880 points
22,097 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP