tags:

views:

32

answers:

2

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.

+1  A: 

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']))

webbiedave
I just edited my question with some more details about why I think it's a bug.
jsdalton
I don't see how your added code redefines it as a bug. It's a behavior decision which you, and many others, disagree with.
webbiedave
Fair enough. "Bug" is the perhaps the wrong choice of words. It is a poor design decision IMO, because I believe functions that encode and decode data should be inversable (where possible at least).
jsdalton
A: 

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.

jsdalton
It would be shorter to just reimplement http_build_query. I was about to recommend http_build_str() from the pecl_http extension. But that doesn't work there either.
mario