PHP function to build query string from array

后端 未结 5 469
暖寄归人
暖寄归人 2020-11-28 20:55

I\'m looking for the name of the PHP function to build a query string from an array of key value pairs. Please note, I am looking for the built in PHP function to d

相关标签:
5条回答
  • 2020-11-28 21:02

    As this question is quite old and for PHP, here is a way to do it in the (currently) very popular PHP framework Laravel.

    To encode the query string for a path in your application, give your routes names and then use the route() helper function like so:

    route('documents.list.', ['foo' => 'bar']);
    

    The result will look something like:

    http://localhost/documents/list?foo=bar
    

    Also be aware that if your route has any path segment parameters e.g. /documents/{id}, then make sure you pass an id argument to the route() parameters too, otherwise it will default to using the value of the first parameter.

    0 讨论(0)
  • 2020-11-28 21:04

    Just as addition to @thatjuan's answer.
    More compatible PHP4 version of this:

    if (!function_exists('http_build_query')) {
        if (!defined('PHP_QUERY_RFC1738')) {
            define('PHP_QUERY_RFC1738', 1);
        }
        if (!defined('PHP_QUERY_RFC3986')) {
            define('PHP_QUERY_RFC3986', 2);
        }
        function http_build_query($query_data, $numeric_prefix = '', $arg_separator = '&', $enc_type = PHP_QUERY_RFC1738)
        {
            $data = array();
            foreach ($query_data as $key => $value) {
                if (is_numeric($key)) {
                    $key = $numeric_prefix . $key;
                }
                if (is_scalar($value)) {
                    $k = $enc_type == PHP_QUERY_RFC3986 ? urlencode($key) : rawurlencode($key);
                    $v = $enc_type == PHP_QUERY_RFC3986 ? urlencode($value) : rawurlencode($value);
                    $data[] = "$k=$v";
                } else {
                    foreach ($value as $sub_k => $val) {
                        $k = "$key[$sub_k]";
                        $k = $enc_type == PHP_QUERY_RFC3986 ? urlencode($k) : rawurlencode($k);
                        $v = $enc_type == PHP_QUERY_RFC3986 ? urlencode($val) : rawurlencode($val);
                        $data[] = "$k=$v";
                    }
                }
            }
            return implode($arg_separator, $data);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 21:14

    Here's a simple php4-friendly implementation:

    /**
    * Builds an http query string.
    * @param array $query  // of key value pairs to be used in the query
    * @return string       // http query string.
    **/
    function build_http_query( $query ){
    
        $query_array = array();
    
        foreach( $query as $key => $key_value ){
    
            $query_array[] = urlencode( $key ) . '=' . urlencode( $key_value );
    
        }
    
        return implode( '&', $query_array );
    
    }
    
    0 讨论(0)
  • 2020-11-28 21:20

    Implode will combine an array into a string for you, but to make an SQL query out a kay/value pair you'll have to write your own function.

    0 讨论(0)
  • 2020-11-28 21:23

    You're looking for http_build_query().

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