Saturday, 21 January 2012

Make a query string from an array

You have an array like this:

$vars = array(
    'id'=>$id,
    'name'=>$name,
    'phone'=>$phone,
    'email'=>$email,
    'subject'=>$subject,
    'notes'=>$notes,
    'time'=>date('Y-m-d H:m:s'),
);

And you want to pass these values into a URL. Well, its pretty easy to do this. Just for the sake of it, lets just sort the array according to the key values

ksort($var);


We can make use of php's http_build_query() function

$query = http_build_query($vars);

echo $query;


You get something like this

email=indrajithiv%40gmail.com&id=20120119221410-4e78f585bc081f694efa8219df22c1a5&name=Indrajith+Vijayakumar¬es

No comments:

Post a Comment