Using arrays : PHP array extract on newest questions tagged arrays – Stack Overflow

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_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;
}

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..

See Answers


source: http://stackoverflow.com/questions/4429738/php-array-extract
Using arrays : using-arrays



online applications demo