http_build_query ignores the key if the value is an empty array. How is this not a bug?
<p>I ran into a problem today where I was passing a key with the value set to an empty array to <code>http_build_query()</code>. E.g.:</p>

<pre><code>$args = array("foo", "bar", array(), "baz");
$qs = http_build_query($args);
echo $qs; // outputs 0=foo&amp;1=bar&amp;3=baz, I expected 0=foo&amp;1=bar&amp;2=&amp;3=baz
</code></pre>

<p>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.</p>

<p>Well, I googled this and came up with the following bug report: <a href="http://bugs.php.net/bug.php?id=50407" rel="nofollow">http://bugs.php.net/bug.php?id=50407</a> . The terse reply from an admin is, "Not setting is same as setting it empty. No bug."</p>

<p>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?</p>

<p>EDIT</p>

<p>Here's why I think it's a bug:</p>

<pre><code>$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
</code></pre>

<p>I guess perhaps it's naive of me to consider <code>parse_str()</code> and <code>http_build_query()</code> to be inverses of one another.</p>

<p>I'm posting my current "lame hack" solution as an answer below.</p>
