我有一个查询字符串:PHP数组提取
"condition=good;condition=not-good&features=ABS&features=ESP&features=ENT&brand=Honda&model=Traffic"
* 请注意重复的参数
我使用此功能转换和 - 获取也重复键 - 阵列:
function proper_parse_str($str) {
# result array
$arr = array();
# split on outer delimiter
$pairs = explode('&', $str);
# loop through each pair
foreach ($pairs as $i) {
# split into name and value
list($name,$value) = explode('=', $i, 2);
# if name already exists
if(isset($arr[$name])) {
# stick multiple values into an array
if(is_array($arr[$name])) {
$arr[$name][] = $value;
}
else {
$arr[$name] = array($arr[$name], $value);
}
}
# otherwise, simply stick it in a scalar
else {
$arr[$name] = $value;
}
}
# return result array
return $arr;
}
为了回显html我使用这个:
//using the above function
$array=proper_parse_str($string);
foreach ($array as $key => $value) {
if (is_array($value)) {
foreach($value as $t) {
$e .="<li>".$t."</li>";
}
$mkey .="<ul><li><b>".$key."</b><ul>".$e."</ul></li></ul>";
} else {
$tt ="<li>".$value."</li>";
$mkey .="<ul><li><b>".$key."</b><ul>".$tt."</ul></li></ul>";
}
}
echo $mkey;
获得:
Condition
good
not-good
Features
ABS
ESP
ENT
Brand
Honda
Model
Traffic
,但我得到:
Condition
good
not-good
Features
**good
**not-good
ABS
ESP
ENT
Brand
Honda
Model
Traffic
请帮助我..
为什么前两对由a分开;代替 &? – Nedec 2010-12-13 14:27:35
设置值'$ k'在哪里? – 2010-12-13 14:28:39
做一个'print_r($ arr)'来检查你的构造是否确实是正确的。打印输出代码看起来有点不完整。 – poke 2010-12-13 14:29:33