在URL中传递多个参数的可能性?

I'm working on a search-based website, and am trying to pass parameters using SEO-friendly URLs.

Is it possible to pass the following URL and get the URL in CodeIgniter?

http://www.example.com/search/prize/10/13/14.5/size/xl/2xl/color/black/white-grey/

I am able to create the URL, but I want to get the URL values like $prize = array("10","13","14.5"), $size= array("xl","2xl"), and $color = array("black","white-grey").

I tried to use the uri_to_assoc() function, but it didn't work. I get the following output:

[price] => 10,
[13] => 14.5
...

which is wrong.

Note: I tried to use $this->uri->segment(1), etc., but in this case, the segment position is dynamic.

For example, users may search for only prices of $10, so the URL will get changed to:

http://www.example.com/search/prize/10/size/xl/2xl/color/black/white-grey/

Now the segment position of getting the size must be changed. In this case, I want:

$prize = array("10");
$size = array("xl", "2xl");
$color = array("black", "white-grey");

How can I achieve this?

You are using quite the unconventional "friendly URI" format. Normally when passing parameters there is a single identifier and then the parameter e.g. /name/key/name/key/name/key.

When you use the correct format /name/key/name/key/name/key in conjunction with uri_to_assoc(), you would get:

array(
    'name' => 'key',
    // etc...
)

but using something like /prize/1/2/3/size/s/m/l/color/black/white/grey would yield:

array(
    'prize' => 1,
    2 => 3,
    'size' => 's',
    'm' => 'l',
    // etc...
)

Which is useless to you.

You will have to fetch all of the segments individually and build your arrays with a foreach:

$segments = $this->uri->segment_array();
$prize = array();
$size = array();
$color = array();
$curKey = '';
foreach ($segments as $seg) {
    if (in_array($seg, array('prize', 'size', 'color'))) {
        $curKey = $seg; continue;
    }
    if (!empty($curKey)) ${$curKey}[] = $seg;
}

// use $prize, $size, and $color as you wish

or alternatively using a multi-dimensional array:

$parameters = array(
    'prize' => array(),
    'size' => array(),
    'color' => array(),
);
$segments = $this->uri->segment_array();
$curKey = '';
foreach ($segments as $seg) {
    if (in_array($seg, array('prize', 'size', 'color'))) {
        $curKey = $seg; continue;
    }
    if (!empty($curKey)) $parameters[$curKey][] = $seg;
}
  1. Fetch all the segments
  2. Iterate over them, check each for keywords like size, color, etc. (make a whitelist)
  3. If a keyword is found, assume the segment values until the next keyword segment or the end are the values for that specific keyword (so xl and 2xl will denote the size if preceeded with a keyword size, etc.)