0

所以我有这个示例字符串:

?items=3744130|1^356221|2^356222|1

从 URL 中提取:

http://www.example.com/process.php?items=3744130|1^356221|2^356222|1

我需要把它转换成这个,对于我的生活,我被卡住了。

?items=model_1=3744130&qty_1=1&model_2=356221&qty_2=2&model_3=356222&qty_3=1

我已经做到了这一点,但仅此而已。

$url = substr($_SERVER['QUERY_STRING'],6); 
$query = explode('^', $url); 
echo http_build_query($query, 'model_');

目前的输出是:

model_0=1234435%7C9&model_1=56788%7C9&model_2=6758765%7C9&model_3=3736543%7C9

我怎样才能得到第一组model_1而不是默认值model_0

谢谢

4

3 回答 3

1

http_build_query()如果您希望编号以 1 开头,则需要在将数组传递给之前使用键构建数组。

$arr = array();
$count = 1;

foreach (explode('|', $_GET['items']) as $item) {
  $elements = explode('^', $item);
  if (count($elements) == 1) {
    $arr["model_$count"] = $elements[0];
    $arr["qty_$count"] = 1;
  }
  else {
    $arr["model_$count"] = $elements[1];
    $arr["qty_$count"] = $elements[0];
  }
  $count++;
}

$query = http_build_query($arr);
于 2009-11-17T09:44:14.703 回答
0

应该是一个快速补丁

....
$url = substr($_SERVER['QUERY_STRING'],6);
$query = explode('^', $url);

array_unshift($query,'placeholder');
$result=http_build_query($query, 'model_');
$result = substr($result, 19);
....
于 2009-11-17T09:22:54.903 回答
0

尝试这个:

$items = explode('^', $_GET['items']);
$query = '';
foreach ($items as $key => $val) {
    list($model, $qty) = explode('|', $val);
    $query .= '&model_'.($key+1).'='.urlencode($model);
    $query .= '&qty_'.($key+1).'='.urlencode($qty);
}
echo substr($query, 1);
于 2009-11-17T09:23:35.593 回答