Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

http_build_query with same name parameters

Tags:

Is there a way to build a query automatically with http_build_query using parameters with the same name?

If I do something like

array('foo' => 'x', 'foo' => 'y'); 

They are obviously overwritten within the array, but even if I do:

array('foo' => array('x', 'y')); 

The function creates something like foo[0]=x&foo[1], which isn't what I want, since I need the parameters in this format foo=x&foo=y.

like image 468
entropid Avatar asked Nov 17 '11 16:11

entropid


People also ask

What is $_ server Query_string?

$_SERVER['QUERY_STRING'] Returns the query string if the page is accessed via a query string. $_SERVER['HTTP_ACCEPT'] Returns the Accept header from the current request. $_SERVER['HTTP_ACCEPT_CHARSET']

What is Http_build_query?

The http_build_query() function is an inbuilt function in PHP which is used to generate URL-encoded query string from the associative (or indexed) array.

What is query string in PHP with example?

A query string commonly includes fields added to a base URL by a Web browser or other client application, for example as part of an HTML, choosing the appearance of a page, or jumping to positions in multimedia content.

How do I create a query string?

An easy way to build a query string in Javascript is to use a URLSearchParams object: var query = new URLSearchParams(); query. append("KEY", "VALUE");


1 Answers

This should do what you want, I had an api that required the same thing.

$vars = array('foo' => array('x','y')); $query = http_build_query($vars, null, '&'); $string = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $query); //foo=x&foo=y 
like image 78
Jason Brumwell Avatar answered Sep 28 '22 08:09

Jason Brumwell