Array to Query with '&' instead of '&apm;' in Laravel 5.5

时间:2017-12-17 08:10:29

标签: php laravel-5.5

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);

1 个答案:

答案 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

Reference

Edit 1:

You may alternatively use:

echo http_build_query($data, '', '&');