PHP function to build query string from array

后端 未结 5 482
暖寄归人
暖寄归人 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: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 );
    
    }
    

提交回复
热议问题