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

前端 未结 5 1147
死守一世寂寞
死守一世寂寞 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:20

    Building on @anyx solution: If you want to preserve the original array and also handle NULL values, you can use this version:

    function empty2blank(array $arr) {
     array_walk($arr, function(&$val, $key) {
      if (empty($val)) { 
       $val = is_array($val) ? '[]' : '';
      } elseif (is_array($val)) {
       $val = empty2blank($val);
      }
     });
     return $arr;
    }
    

提交回复
热议问题