Voting

: seven plus one?
(Example: nine)

The Note You're Voting On

Mark Simon
10 years ago
As noted, this function omits keys with null values. This could break some code which treats the key as boolean, and so has no value, or other code expecting the array to be populated regardless of value.

A workaround for this is to replace the null values with an empty string:

$data=array(
'a'=>'apple',
'b'=>2,
'c'=>null,
'd'=>'…',
);

// Compensate for fact that http_build_query omits null values
foreach($data as &$datum) if($datum===null) $datum='';

Losing the null-ness of the original is no real loss if it’s supposed to be a real query string. If the null is important, you could use a dummy value instead.

Mark

<< Back to user notes page

To Top