http_build_query ignores the key if the value is an empty array. How is this not a bug?

前端 未结 5 1230
死守一世寂寞
死守一世寂寞 2021-02-13 13:16

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\         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-13 13:36

    You can simply walk the query params, if empty array, use "[]" instead, like this:

    function walkCriteria(&$criteria) {
        array_walk($criteria, function (&$val) {
            if ($val === []) {
                $val = "[]";
            } else if (is_array($val)) {
                walkCriteria($val);
            }
        });
    }
    

    Don't use array_walk_recursive. Because it will walk into the empty array and do nothing.

提交回复
热议问题