Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按PHP的确切顺序添加Uri参数_Php_Sorting_Parameters_Uri - Fatal编程技术网

按PHP的确切顺序添加Uri参数

按PHP的确切顺序添加Uri参数,php,sorting,parameters,uri,Php,Sorting,Parameters,Uri,我想问一下,如何准确地排序/添加URI参数,以避免使用相同的查询重复地址。对于specify,我正在对产品的品牌进行分类 例如: https://www.example.me/page?b:0=foo&b:2=bar 当用户选择另一个参数时,它将显示如下: https://www.example.me/page?b:0=foo&b:2=bar&b:1=moe https://www.example.me/page?b:0=foo&b:1=moe&b:2

我想问一下,如何准确地排序/添加URI参数,以避免使用相同的查询重复地址。对于specify,我正在对产品的品牌进行分类

例如:

https://www.example.me/page?b:0=foo&b:2=bar
当用户选择另一个参数时,它将显示如下:

https://www.example.me/page?b:0=foo&b:2=bar&b:1=moe
https://www.example.me/page?b:0=foo&b:1=moe&b:2=bar
但无论用户如何选择param,我都想要完全相同的位置,如下所示:

https://www.example.me/page?b:0=foo&b:2=bar&b:1=moe
https://www.example.me/page?b:0=foo&b:1=moe&b:2=bar
首先我从mysql收集数据到一个数组,然后数组按ksort()排序,但它只对其中的值排序,而不排序链接的生成方式

 $actual_link = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
 $delim =  (strpos($_SERVER['REQUEST_URI'], "?")) ? "&" : "?";

 while ($line = mysqli_fetch_array($stmt)) {

                $links[$x]['name']      = $line['brand_name'];

                if (strpos($actual_link, "b:".$x."=".$line['brand_tag'])) {
                    // this param is checked and exists in URL

                    $new_actual_link = str_replace('&', '?', preg_replace('#[&?]b:'.$x.'=[^&]*#', null, $actual_link));

                    $links[$x]['checked']   = true;
                    $links[$x]['link']      = $new_actual_link;
                }
                else {
                    // this param is not chosen, not exists in URL
                    $links[$x]['link'] = $actual_link.$delim."b:".$x."=".$line['brand_tag'];
                }
                $x++;
            }

            ksort($links);
            foreach ($links as $link) {...}

如果您要控制请求的格式,则使用一个参数作为数组(即b=foo、bar、baz)可能会更容易。根据我的经验,重复的参数名是有效的,但在实现中确实不一致。我无法遵循此脚本。为什么不使用http\u build\u query()?类似数组的数据最好作为数组存储/传递。使用大括号语法或让函数为您正确执行。为什么您的sql不进行排序?这看起来像是一个错误。我看到您发现了这一点:但是您应该引用字符串键;请查看第二高的答案。如果您总是超链接到当前页面,只需更改并显示查询字符串。