I'm trying to build a query from an array.
I did try to use http_build_query
function but it results &
instead of &
So, I try to create my own function but I have this error:
Object of class Symfony\Component\HttpFoundation\ParameterBag could not be converted to string
This is my code:
$params = [
'foo' => $reuest->foo,
'baz' => 'boom',
'cow' => 'milk',
'php' => 'hypertext processor']
$paramsJoined = array();
foreach($params as $param => $value) {
$paramsJoined[] = "$param=$value";
}
$query = implode('&', $paramsJoined);
答案 0 :(得分:3)
This is an example of what you are trying to achieve:
<?php
$data = [
'foo' => 'bar',
'baz' => 'boom',
'cow' => 'milk',
'php' => 'hypertext processor'];
echo http_build_query($data);
?>
Output:
foo=bar&baz=boom&cow=milk&php=hypertext+processor
Edit 1:
You may alternatively use:
echo http_build_query($data, '', '&');