[php-src] Issue #13802: Optional new interpretation for list parameters in parse_str function
| From: | letibelim | Date: | Mon, 25 Mar 2024 10:45:59 +0000 |
| Subject: | [php-src] Issue #13802: Optional new interpretation for list parameters in parse_str function | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-246646@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/13802
Author: letibelim
### Description
**Description**
The [parse_str](https://www.php.net/manual/en/function.parse-str.php) function could have an
optional parameter allowing a string like :
'foo=bar&foo=baz' to be
interpreted as:
```php
array(1) {
["foo"]=>
array(2) {
[0]=>
string(3) "bar"
[1]=>
string(3) "baz"
}
}
```
instead of relying on square brackets to have this effect.
As of today, only the last argument is taken into account:
The following snippet
```php
$result = [];
$string = 'foo=bar&foo=baz';
parse_str($string, $result);
var_dump($result);
```
gives the following result:
```pĥp
array(1) {
["foo"]=>
string(3) "baz"
}
```
And one needs to use square brackets to get the intended result:
```php
$result = [];
$string = 'foo[]=bar&foo[]=baz';
parse_str($string, $result);
var_dump($result);
```
This has been pointed out in [a comment](https://www.php.net/manual/en/function.parse-str.php#76792)
a long time ago as being a php specific behaviour :
```pĥp
array(1) {
["foo"]=>
array(2) {
[0]=>
string(3) "bar"
[1]=>
string(3) "baz"
}
}
```
I feel this is important, as the interpretation of the query string is built on the parse_str
function. And sometimes, one cannot choose the API signature that needs to be implemented and cannot
leverage the []. While one could definitely uses custom alternatives of parse_str (see
further comments), some frameworks rely on this function to build request Objects (namely Symfony,
but probably others, too).
**Example**
an optional boolean argument with a default value (for backward compatibility) of the parse_str
function could allow the more classical interpretation.
Suggested signature of the parse_str function:
```php
parse_str(string $string, array &$result, $brackets = true): void
```