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

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

    I've reimplemented http_build_query to leave empty objects/arrays in the returned query string (suffixed by an '=' symbol). I've enhanced it a bit from the default functionality as well, so all-in-all:

    • Maintains empty objects and arrays
    • Changed the default enc_type to RFC3986 (relevant to the ages)
    • Added a key-value separator argument (ability to override the default '=')
    • Removes numeric indices for numerically-indexed key-value pairs

    I have not tested this in a production environment (no idea about performance or bugs), and it is not optimized, but very well spelled out.

    function buildQuery($input,$numeric_prefix='',
            $arg_separator='&',$enc_type=2,
            $keyvalue_separator='=',$prefix='') {
        if ( is_array($input) ) {
            $arr = array();
            foreach ( $input as $key => $value ) {
                $name = $prefix;
                if ( strlen($prefix) ) {
                    $name .= '[';
                    if ( !is_numeric($key) ) {
                        $name .= $key;
                    }
                    $name .= ']';
                } else {
                    if ( is_numeric($key) ) {
                        $name .= $numeric_prefix;
                    }
                    $name .= $key;
                }
                if ( (is_array($value) || is_object($value)) && count($value) ) {
                    $arr[] = buildQuery($value,$numeric_prefix,
                            $arg_separator,$enc_type,
                            $keyvalue_separator,$name);
                } else {
                    if ( $enc_type === 2 ) {
                        $arr[] = rawurlencode($name)
                            .$keyvalue_separator
                            .rawurlencode($value?:'');
                    } else {
                        $arr[] = urlencode($name)
                            .$keyvalue_separator
                            .urlencode($value?:'');
                    }
                }
            }
            return implode($arg_separator,$arr);
        } else {
            if ( $enc_type === 2 ) {
                return rawurlencode($input);
            } else {
                return urlencode($input);
            }
        }
    }
    

    Example:

    $arr = array(
            'hello' => 'world',
            'colors' => array('red','green','blue'),
            'emptyArr' => array(),
            'nested' => array(
                'empty' => array(),
                'fruits' => array('orange','banana','apple'),
                'curly' => 'sue',
                'assoc' => array('a'=>'alpha','b'=>'bravo','c'=>'charlie')
            )
        );
    
    echo buildQuery($arr);
    

    Outputs: hello=world&colors%5B%5D=red&colors%5B%5D=green&colors%5B%5D=blue&emptyArr=&nested%5Bempty%5D=&nested%5Bfruits%5D%5B%5D=orange&nested%5Bfruits%5D%5B%5D=banana&nested%5Bfruits%5D%5B%5D=apple&nested%5Bcurly%5D=sue&nested%5Bassoc%5D%5Ba%5D=alpha&nested%5Bassoc%5D%5Bb%5D=bravo&nested%5Bassoc%5D%5Bc%5D=charlie I hope this finds someone well.

    0 讨论(0)
  • 2021-02-13 13:20

    Can somebody explain to me why this is not a bug?

    Technically, I don't think it should be labeled a bug. Rather, it's just how they designed the function to behave, whether others disagree with that decision or not.

    Your API could just check with if (empty($_POST['2']))

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2021-02-13 13:27

    This is my current "lame hack" solution. Note I had to account for the possibility of nested arrays, so my example original array is slightly different from what I posted in the question:

    $args = array("foo", "bar", array("red", "blue", array(), "green"), "baz");
    $original_array = $args; // save it to compare later
    function replace_empty_array_with_fake_string(&$value, $key) {
        if (is_array($value)) {
            if (empty($value)) {
                $value = 'array()';
            } else {
                array_walk($value, 'replace_empty_array_with_fake_string');
            }
    
        }
    }
    array_walk($args, 'replace_empty_array_with_fake_string');
    $qs = http_build_query($args);
    
    // convert the query string back to an array, this would happen on the "other side"
    parse_str($qs, $query);
    function replace_fake_string_with_empty_array(&$value, $key) {
        if ($value == 'array()') {
            $value = array();
        }
        if (is_array($value)) {
            array_walk($value, 'replace_fake_string_with_empty_array');
        }
    }
    array_walk($query, 'replace_fake_string_with_empty_array');
    echo ($original_array == $query); // true
    

    Presumably I could come up with a more arbitrary string than "array()" to use as the placeholder.

    Lame, I know.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题