php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #80536 http_build_query dense numerically indexed arrays without brackets flag?
Submitted: 2020-12-20 16:56 UTC Modified: -
From: divinity76 at gmail dot com Assigned:
Status: Open Package: URL related
PHP Version: Next Minor Version OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: divinity76 at gmail dot com
New email:
PHP Version: OS:

 

 [2020-12-20 16:56 UTC] divinity76 at gmail dot com
Description:
------------
there are some apis which doesn't support http_build_query()'s array method of foo[0]=a&foo[1]=b&foo[2]=c, instead specifying arrays like foo=a&foo=b&foo=c  ,

one such api is Google's Closure Compiler Rest API, https://developers.google.com/closure/compiler/docs/gettingstarted_api

i recently had to generate a POST request to that api through PHP, i ended up doing
```
"output_info=compiled_code&output_info=warnings&output_info=errors&output_info=statistics&" . http_build_query(array(
        'output_format' => 'json',
        'compilation_level' => $compile_level,
        'warning_level' => 'verbose',
        'js_code' => $js_to_minify
    ))
```
i had to hack the output_info array data in there, instead of doing it the "proper way" of 
```
http_build_query(array(
    'output_format' => 'json',
    'compilation_level' => $compile_level,
    'warning_level' => 'verbose',
    'js_code' => $js_to_minify,
    'output_info' => array('compiled_code', 'warnings', 'errors', 'statistics')
))
```

it would be nice if http_build_query() could get a flag to treat numerically indexed densely populated arrays (eg what you get from array_values() or array("a","b","c") ) without brackets.. (there's probably a better name for it, idk what)

not sure what the flag name should be though, PHP_QUERY_DENSE_NUMERICALLY_INDEXED_ARRAYS_WITHOUT_BRACKETS kinda feels too long

Test script:
---------------
<?php
echo http_build_query(
 ["foo"=>["a","b","c"]],
 null,
 '&',
 PHP_QUERY_DENSE_NUMERICALLY_INDEXED_ARRAYS_WITHOUT_BRACKETS
);

Expected result:
----------------
foo=a&foo=b&foo=c


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-12-31 16:37 UTC] antonino dot spampinato86 at gmail dot com
you can't because to recover php use the super global array $_GET and overwrite the last key.
http://exampla.domain.com/index.php?foo=0&foo=1 $_GET['foo'] // 1 not 0 and 1
use https://3v4l.org/ku2oK
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Dec 03 11:03:36 2021 UTC