I ran into a problem today where I was passing a key with the value set to an empty array to http_build_query()
. E.g.:
$args = array("foo", "bar", array(), "baz");
$qs = http_build_query($args);
echo $qs; // outputs 0=foo&1=bar&3=baz, I expected 0=foo&1=bar&2=&3=baz
This presents a problem for me, since I'm passing some data to an internal API over http and I need to pull all the arguments out on the other side.
Well, I googled this and came up with the following bug report: http://bugs.php.net/bug.php?id=50407 . The terse reply from an admin is, "Not setting is same as setting it empty. No bug."
Can somebody explain to me why this is not a bug? Does anyone have any ideas for a workaround, aside from the lame hack of setting an arbitrary value on one side and interpreting that value as an empty value on the other?
EDIT
Here's why I think it's a bug:
$args = array("foo", "bar", array(), "baz");
$qs = http_build_query($args);
parse_str($qs, $query);
echo ($args == $query); // false, I expect it to be true
I guess perhaps it's naive of me to consider parse_str()
and http_build_query()
to be inverses of one another.
I'm posting my current "lame hack" solution as an answer below.
I've reimplemented http_build_query
to leave empty objects/arrays in the returned query string (suffixed by an '=' symbol). I've enhanced it a bit from the default functionality as well, so all-in-all:
I have not tested this in a production environment (no idea about performance or bugs), and it is not optimized, but very well spelled out.
function buildQuery($input,$numeric_prefix='',
$arg_separator='&',$enc_type=2,
$keyvalue_separator='=',$prefix='') {
if ( is_array($input) ) {
$arr = array();
foreach ( $input as $key => $value ) {
$name = $prefix;
if ( strlen($prefix) ) {
$name .= '[';
if ( !is_numeric($key) ) {
$name .= $key;
}
$name .= ']';
} else {
if ( is_numeric($key) ) {
$name .= $numeric_prefix;
}
$name .= $key;
}
if ( (is_array($value) || is_object($value)) && count($value) ) {
$arr[] = buildQuery($value,$numeric_prefix,
$arg_separator,$enc_type,
$keyvalue_separator,$name);
} else {
if ( $enc_type === 2 ) {
$arr[] = rawurlencode($name)
.$keyvalue_separator
.rawurlencode($value?:'');
} else {
$arr[] = urlencode($name)
.$keyvalue_separator
.urlencode($value?:'');
}
}
}
return implode($arg_separator,$arr);
} else {
if ( $enc_type === 2 ) {
return rawurlencode($input);
} else {
return urlencode($input);
}
}
}
Example:
$arr = array(
'hello' => 'world',
'colors' => array('red','green','blue'),
'emptyArr' => array(),
'nested' => array(
'empty' => array(),
'fruits' => array('orange','banana','apple'),
'curly' => 'sue',
'assoc' => array('a'=>'alpha','b'=>'bravo','c'=>'charlie')
)
);
echo buildQuery($arr);
Outputs: hello=world&colors%5B%5D=red&colors%5B%5D=green&colors%5B%5D=blue&emptyArr=&nested%5Bempty%5D=&nested%5Bfruits%5D%5B%5D=orange&nested%5Bfruits%5D%5B%5D=banana&nested%5Bfruits%5D%5B%5D=apple&nested%5Bcurly%5D=sue&nested%5Bassoc%5D%5Ba%5D=alpha&nested%5Bassoc%5D%5Bb%5D=bravo&nested%5Bassoc%5D%5Bc%5D=charlie
I hope this finds someone well.
Can somebody explain to me why this is not a bug?
Technically, I don't think it should be labeled a bug. Rather, it's just how they designed the function to behave, whether others disagree with that decision or not.
Your API could just check with if (empty($_POST['2']))
This is my current "lame hack" solution. Note I had to account for the possibility of nested arrays, so my example original array is slightly different from what I posted in the question:
$args = array("foo", "bar", array("red", "blue", array(), "green"), "baz");
$original_array = $args; // save it to compare later
function replace_empty_array_with_fake_string(&$value, $key) {
if (is_array($value)) {
if (empty($value)) {
$value = 'array()';
} else {
array_walk($value, 'replace_empty_array_with_fake_string');
}
}
}
array_walk($args, 'replace_empty_array_with_fake_string');
$qs = http_build_query($args);
// convert the query string back to an array, this would happen on the "other side"
parse_str($qs, $query);
function replace_fake_string_with_empty_array(&$value, $key) {
if ($value == 'array()') {
$value = array();
}
if (is_array($value)) {
array_walk($value, 'replace_fake_string_with_empty_array');
}
}
array_walk($query, 'replace_fake_string_with_empty_array');
echo ($original_array == $query); // true
Presumably I could come up with a more arbitrary string than "array()" to use as the placeholder.
Lame, I know.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With