0

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

4
  • Why are the first two pairs separated by a ; instead of a &? Commented Dec 13, 2010 at 14:27
  • Where is the value $k being set? Commented Dec 13, 2010 at 14:28
  • Do a print_r( $arr ) to check if your construction is actually correct. The print-out code looks a bit incomplete. Commented Dec 13, 2010 at 14:29
  • @Nadec: I would say type since there's nothing in the code to look for it. Commented Dec 13, 2010 at 14:31

5 Answers 5

5

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

Sign up to request clarification or add additional context in comments.

Comments

1

Why not use parse_str? (Assuming what you're doing is typical to parsing a GET argument string).

3 Comments

It's not identical (I was going to say the same thing, but then realized the difference). Basically this simulates array syntax without the need for []. With parse_str, identical keys overwrite, so foo=bar&foo=baz would only result in foo=baz. But his implementation would transform it to foo[]=bar&foo[]=baz. Slightly different (and not fitting with the spec), but it is different...
Ah, good catch. Seems almost easier to catch these duplicates and adjust them, then parse with the native function. I guess to each their own.
That's what I would likely do if I had to solve this problem myself. That way you're still coming up with valid query strings, and have the benefit of the decoding/etc that the native function gives...
0
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>";

1 Comment

Manny smart / experts guys here...thank's this solution works too.
0

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)

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.