convert parameters to query string

    0

    1

    JVIT PHP

    This function takes an array of parameter name/value pairs and converts them into a query string. The query string will be the concatenation of the parameter names and their values, with each value URL-encoded.

    Shortcut: url.convertToQueryString

    
    /**
    * Converts an array of parameters name => value mappings to a query
    * string. Values must already be URL encoded before you call this function.
    *
    * @param array $parameters eg. `array('param1' => 10, 'param2' => array(1,2))`
    * @return string eg. `"param1=10&param2[]=1&param2[]=2"`
    * @api
    */
    function convertParametersToQueryString($parameters)
    {
      $query = '';
      foreach ($parameters as $name => $value) {
        if (is_null($value) || $value === false) {
          continue;
        }
        if (is_array($value)) {
          foreach ($value as $theValue) {
            $query .= $name . "[]=" . $theValue . "&";
          }
        } else {
          $query .= $name . "=" . $value . "&";
        }
      }
      $query = substr($query, 0, -1);
      return $query;
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.