http_build_query with same name parameters
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
http_build_query giving me get parameters of the same name
Your $args
variable should look like:
$args = array (
'pricefrom' => $fromval,
'priceto' => $toval,
'model' => $array
);
UPD
Use preg_replace
for replace html special chars if you want to use http_build_query
with multiple params.
$query = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '[]=', http_build_query($args));
You will receive an array by accessing to $_GET['model']
How to set query parameters with same name as Guzzle?
I solved above mentioned problem using Guzzle query request params.
I changed the query params as below. Then I changed the value of the guzzle 'query'.
$query_params = [
'test' => 'abc',
'test2' => true,
'limit' => 10,
'item_id' => array('8159','123')
];
$response = $this->client->request('GET', $endpoint, [
'headers' => [
'X-API-KEY' => KEY
],
'query' => Query::build($query_params)
]);
How to construct url with http_build_query, but display values without variables?
You could use implode():
$server = $_GET["server"];
$data = [$_GET["z_koord"],
$_GET['x_koord'],
$_GET["y_koord"]];
$url = '.tile.openstreetmap.org';
$saite = "$server/$url/" . implode('/', $data) . ".png";
I'm not sure about some things in this code, but the implode()
should do the job.
PHP http_build_query() generate incorrect url for array get parameter
It's not a bug to http_build_query() function.
When you pass the get parameter via URL like: "arrayName[]="
print_R($_GET);
will return
Array
(
[arrayName] => Array
(
[0] =>
)
)
and http_build_query generates the url encoded string from array, the result is:
&arrayName%5B0%5D=
and decoded this looks like:
arrayName[0]=
Now you can see where the 0 came from :)
There is no need to fix this, you can change your code to pass keys for the arrayName or still use it as is.
PHP - Using http_build_query for pagination is giving me repeated parameters
Use array_unique function
$_GET = array_unique ( $_GET );
Or better use preg_replace function:
$query = http_build_query($_GET);
$query = preg_replace('/page=\d*/i', '', $query);
So
<?php
if($pages > 1 && $set <= $pages) {
$query = http_build_query($_GET);
$query = preg_replace('/page=\d*/i', '', $query);
?>
<div class="pagination-bar">
<?php
if($set > 1) {
?>
<a href="?<?php echo $query ?>&page=1"> <<< </a>
|
<a href="?<?php echo $query ?>&page=<?php echo ((int)$set - 1) ?>"><<</a>
|
<?php
}
for($i = 1; $i <= $pages; $i++) {
?>
<a href="?<?php echo $query ?>&page=<?php echo $i.""; ?>"><?php echo $i; ?></a> |
<?php
}
if($set < $pages) {
?>
<a href="?<?php echo $query ?>&page=<?php echo ((int)$set + 1); ?>">>></a>
|
<a href="?<?php echo $query ?>&action=list&page=<?php echo $pages; ?>"> >>> </a>
<?php
}
?>
</div>
<?php
}
http_build_query() without url encoding
You can use urldecode()
function on a result string which you get from http_build_query()
http_build_query without value on parameters (null value)
Can I achieve this result with http_build_query?
No. Internally http_build_query
appends the =
key-value separator no matter what is the value of the parameter.
You can see the source code here (PHP 7.3.3)
Means you either need to accept the cancel-renewal=
look of the parameter or may be redesign the path to have something like /mypage/cancel-renewal?subscription-id=42
Third option would be to write your own simple function to build the query string.
Add, change, and remove GET parameters from query string
function changeQueryString($queryStr, $updateStr, $removeStr) {
parse_str($queryStr, $queryArr);
parse_str($updateStr, $updateArr);
parse_str($removeStr, $removeArr);
$changedArr = array_merge($queryArr, $updateArr);
foreach($removeArr as $k => $v) {
if(array_key_exists($k, $changedArr)) {
unset($changedArr[$k]);
}
}
return http_build_query($changedArr);
}
$str = 'apple=green&banana=yellow2&navi=blue&clouds=white&car=black';
$changedQuery = changeQueryString($str, 'eyes=2&fingers=10&car=purple', 'clouds&apple');
var_dump($changedQuery);
This should work for you, utilizing parse_str(), array_merge() and http_build_query()
Related Topics
Type-Juggling and (Strict) Greater/Lesser-Than Comparisons in PHP
Generating a Single Entity from Existing Database Using Symfony2 and Doctrine
What Is an Abstract Class in PHP
Autoload Classes from Different Folders
Adding Subscribers to a List Using Mailchimp's API V3
Laravel 5.2 Not Reading Env File
Find First Character That Is Different Between Two Strings
Programmatically Add Product to Cart with Price Change
Double Not (!!) Operator in PHP
Getting the First Character of a String with $Str[0]
PHP Is Not Recognized as an Internal or External Command in Command Prompt
Unit Testing and Static Methods
Asynchronous Function Call in PHP