简体   繁体   中英

How to use build_query function of Guzzle?

I have to pass a query parameters with different values but absolutely equal keys. I found this \GuzzleHttp\Psr7\build_query(); But it returns only last value. For instance:

$array = [
       'test' => '1',
       'test' => '2'
    ]
\GuzzleHttp\Psr7\build_query($array);

//OR

http_query_builder($array);

It returns every time string with the last item.

Does it possible to do that with PHP? Because the side which will take these parameters just can not do anything in their side so I have to pass parameters with the equal keys.

The problem was not with the specific method used, but with how you filled your array to begin with:

$array = [
       'test' => '1',
       'test' => '2'
    ]

You can not use the same array key twice; your array only contains one element now, because the second one has overwritten the existing first one under that same key.

Make the test element itself an array, that contains your two values:

$array = [
  'test' => ['1', '2']
];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM