How can you really rewrite the query string like:

test.php?cat1[]=18&cat1[]=687&xxx[]=5&xxx[]=3&xxx[]=1&yyy[]=6

to

test.php?cat1=18,687,5&xxx=3,1&yyy=6

Observe that the parameters (title and cost pairs) are produced dynamically.

I discovered a strategy to do that transformation without modifying the code.

Within the httpd.conf (during my VirtualHost section) I define a rewrite map:

RewriteMap programmap prg:/var/www/localhost/htdocs/chg.php

Then within the .htaccess I set the next rules:

RewriteEngine On

RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(script.php) $1?${programmap:%1} [L]

$1 are a symbol of first "()" in RewriteRule

%1 are a symbol of first "()" in RewriteCond

I Quickly write this script "/var/www/localhost/htdocs/chg.php" (in PHP but could maintain C, Perl, or whatelse):

#!/usr/bin/php -f
<?php
$pos1 =  2;
$pos2 =  $pos1 + 1;
$reg = '/(([a-z0-9_]+)\[\]=([^&]*))/';
while(true){
        $res=array();
        $buff = trim(fgets(STDIN));
        if(feof(STDIN)){
                break;
        }
        $r = preg_match_all($reg, $buff, $match,PREG_SET_ORDER);
        if($r){
                foreach($match as $row){
                        if(!isset($res[$row[$pos1]])){
                                $res[$row[$pos1]] = $row[$pos1]."=".$row[$pos2];
                        } else {
                                $res[$row[$pos1]] .= ",".$row[$pos2];
                        }
                }
                $out=join('&',$res);
        } else {
                $out=$buff;
        }
        echo "$out\n";
}
if (preg_match('/[\][]/',$_SERVER['QUERY_STRING'])) {
  foreach ($_GET as $key => &$val) {
    $_GET[$key] = is_array($val) ? implode(',', $val) : $val;
  }
  header('Location: test.php?'.rawurldecode(http_build_query(array_filter($_GET))));
}

test.php?cat1=18,687,5&xxx=3,1&yyy=6

attempt to place this function before your code:

url_parsestring2array(& $_GET);

function url_parsestring2array($args)
{
    if (empty($args) || !is_array($args) || !$args) {
        return;
    }
    foreach ($args as $key => $val) {
        $tmp = explode(',', $val);
        if (count($tmp) > 1) {
            $args[$key] = $tmp;
        }
    }
}

var_dump($_GET);

prints

array(3) ["xxx"]=> array(2) ["yyy"]=> string(1) "6"

Here is a short php script that produces the query string that you would like. It is best to avoid this part using mod_rewrite, since it is simply outdoors of this scope:

<?php

$ret = "";
foreach($_GET as $key=>$val) {
  if(is_array($val)) {
    // Create the comma separated string
    $value = $val[0];
    $length = count($val);
    for($i=1; $i < $length; $i++) {
      $value .= ',' . $val[$i];
    }
    $ret .= "$key=$value&";
  } else {
    $ret .= "$key=$val&";
  }
}

// Remove last '&'
$ret = substr($ret , 0, strlen($ret)-1);

// Redirect the browser
header('HTTP/1.1 302 Moved');
header("Location: /test.php?" . $ret);

?>

Should you save that script as /rewrite.php, for instance, you'll be able to include these rules within the .htaccess file to reroute demands with query strings that contains arrays to /rewrite.php:

RewriteCond %{QUERY_STRING} \[\]
RewriteRule ^test.php /rewrite.php [L,QSA]

Then your rewrite.php script will rewrite the query string and redirect the browser using the concatenated query string.