Skip to content

Instantly share code, notes, and snippets.

@andho
Created April 19, 2012 17:56
Show Gist options
  • Save andho/2422621 to your computer and use it in GitHub Desktop.
Save andho/2422621 to your computer and use it in GitHub Desktop.
Convert associative array into a query string
<?php
$params = array(
'query' => 'something',
'page' => 1,
'items' => 10
);
$url = 'http://someapi.com/search';
if (count($params) > 0) {
$url .= '?' . implode('&', array_map(function($item) {
return $item[0] . '=' . $item[1];
}, array_map(null, array_keys($params), $params)));
}
echo $url; // http://someapi.com/search?query=something&page=1&items=10
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment