Implode array to HTTP-GET string

This takes an associative array and returns a string like key1=val1&key2=val2.

<?php
 
  function httpimplode($aryInput, $pre = '', $aft = '') {
    if (!empty($pre)) {
      $result = $pre;
    } else {
      $result = '';
    }
    if (is_array($aryInput) && count($aryInput)>0) {
      foreach ($aryInput as $key=>$value) {
        if (strlen($result)>1) {
          $result .= '&';
        }
        $result .= urlencode($key) . '=' . urlencode($value);
      }
      if (!empty($aft)) {
        $result .= $aft;
      }
    }
    return $result;
  }
 
  $params['lang'] = 'de';
  echo 'http://www.blabla.com' . httpimplode($params, '?', '');
  echo 'http://www.blabla.com' . httpimplode($params, '?', '&') . 'show=all';
 
?>

 
snippets/php/httpimplode.txt · Last modified: 2008-07-15 22:26.09 by mbirth
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki Contents powered by Club-Mate Contents powered by BassDrive.com Labelled with ICRA