NULLを送りたい時の落とし穴の内容
http_build_queryの仕様ではあるが、渡したパラメーター配列の値やキーによって、若干想定(希望w)と異なる結果を返してしまうことがある。
問題点は2つ:
「キーのみあって値がNULL」の場合、http_build_queryの返却値にパラメーターのキーが入らず、パラメーターとして欠けてしまう。
例:
$params = array('key1' => null, 'key2' => 'value2');
var_dump(http_build_query($params));
の場合の結果は
string 'key2=value2' (length=11)
になります。
野望的には
string 'key1=&key2=value2' (length=11)
を期待していても無駄です。
対策は、あとで追記しますが、まずは備忘録的にアップしました。