日期:2014-05-17 浏览次数:20560 次
$pre_url = $_SERVER['HTTP_REFERER']; $t = parse_url($pre_url); $a1 = array(); $a2 = array(); parse_str($t['query'],$a1); parse_str($_SERVER['QUERY_STRING'],$a2); $sort = array('a','b','c','d');//初始排序 if (!empty($a2['sort'])){ $t = explode(',', $a2['sort']);//请求方式 $t1 = array_diff($sort, $t); foreach ($sort as &$v){ if (in_array($v,$t)){ $tmp = array_diff($t, array($v)); if (!empty($tmp)) $v .= ','.implode(',', array_diff($t, array($v))); }else{ $v .= ','.$a2['sort']; } } } echo sprintf(' <a href="%s">clear</a> <a href="?sort=%s">a</a> <a href="?sort=%s">b</a> <a href="?sort=%s">c</a> <a href="?sort=%s">d</a> ',$_SERVER['PHP_SELF'],$sort[0],$sort[1],$sort[2],$sort[3]);
------解决方案--------------------
换了个面孔,加了点注释.
$query_string = $_SERVER['QUERY_STRING'];//获取此次请求的条件串 $sort = array('a=1','b=2','c=3','d=4');//初始条件 $dep = '&';//分隔符,可以用于rewrite if (!empty($query_string)){ $current_options = explode($dep, $query_string);//当前请求条件数组 echo '<pre>'; print_r($current_options);//输出条件,查看当前的条件. echo '</pre>'; foreach ($sort as &$v){//此处循环是用来改变初始条件数组 if (in_array($v,$current_options)){//判断当前数组成员是否已经存在 $tmp = array_diff($current_options, array($v));//差集用来添加 if (!empty($tmp)){ $v .= $dep.implode($dep, $tmp); } }else{//不存在即添加 $v .= $dep.$query_string; } } } echo sprintf(' <a href="%s">clear</a> <a href="?%s">a</a> <a href="?%s">b</a> <a href="?%s">c</a> <a href="?%s">d</a> ',$_SERVER['PHP_SELF'],$sort[0],$sort[1],$sort[2],$sort[3]);
------解决方案--------------------