I have a querystring :
"condition=good;condition=not-good&features=ABS&features=ESP&features=ENT&brand=Honda&model=Traffic"
*please note duplicate parameter
I use this function to convert and - get also duplicate key - to array :
function proper_p开发者_JAVA技巧arse_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;
}
In order to echo html I use this :
//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;
to get :
Condition
good
not-good
Features
ABS
ESP
ENT
Brand
Honda
Model
Traffic
but I get :
Condition
good
not-good
Features
**good
**not-good
ABS
ESP
ENT
Brand
Honda
Model
Traffic
Please help me..
You never initialized $e
in your echoing code. So it will always just append the new values rather than resetting. Try this:
$array=proper_parse_str($string);
$mkey = '';
foreach ($array as $key => $value) {
if (is_array($value)) {
$e = '';
foreach($value as $t){
$e .="<li>".$t."</li>";
}
$mkey .="<ul><li><b>".$k."</b><ul>".$e."</ul></li></ul>";
} else {
$tt ="<li>".$value."</li>";
$mkey .="<ul><li><b>".$key."</b><ul>".$tt."</ul></li></ul>";
}
}
echo $mkey;
The moral of the story: always initialize your variables...
Why not use parse_str? (Assuming what you're doing is typical to parsing a GET argument string).
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);
$arr[$name][] = $value;
}
# return result array
return $arr;
}
//using the above function
$array=proper_parse_str("condition=good&condition=not-good&features=ABS&features=ESP&features=ENT&brand=Honda&model=Traffic");
echo "<ul>";
foreach ($array as $key => $value)
{
echo "<li>$key<ol>";
foreach ( $value as $k => $v )
{
echo "<li>$v</li>";
}
echo "</ol></li>";
}
echo "</ul>";
Can you change your query string? If so, you may want to look at using arrays. E.g.
condition[]=good&condition[]=not-good&features[]=ABS&features[]=ESP&features[]=ENT&brand[]=Honda&model[]=Traffic
gives:
array
'condition' =>
array
0 => string 'good' (length=4)
1 => string 'not-good' (length=8)
'features' =>
array
0 => string 'ABS' (length=3)
1 => string 'ESP' (length=3)
2 => string 'ENT' (length=3)
'brand' =>
array
0 => string 'Honda' (length=5)
'model' =>
array
0 => string 'Traffic' (length=7)
You can do the string generation a bit more simple using an array join.
$mkey = '';
foreach ( proper_parse_str( $string ) as $key => $value )
{
if ( is_array( $value ) )
$value = implode( '</li><li>', $value );
$mkey .= '<li><b>' . $key . '</b><ul><li>' . $value . '</li></ul></li>';
}
echo $mkey;
Regardless of that, I would suggest you to always use an array as the value holder in your array to make your structure more static.
精彩评论