openmind ☃   September 12, 2008  ☃  PHP: url string to array  ()

I’m using codeigniter and jQuery to build a website, and all I can say is, wow, what a combination. However, there’s really no easy way to pass jQuery’s sortable serialize method output to PHP; Mark Quezada has one elegant solution.

The following code owes its thesis to his post; this builds on the idea by wrapping parse_str with a generic function that builds up a hash out of the query string with variable variables.

This is possible because of PHP’s double-dollar sign syntax.

  function url_string_to_array()
  {
    $array = array();
    foreach($_POST as $key => $value)
    {
      print "$key => $value\n";
      parse_str($value, $$key);
      $output[$key] = array_values($$key);
    }
    return $output;
  }

blog comments powered by Disqus