Skip to content

Instantly share code, notes, and snippets.

@SebastienGicquel
Forked from andho/querystring.php
Created October 17, 2016 22:13
Show Gist options
  • Save SebastienGicquel/f328df5282a0966d02c5445a9badcd05 to your computer and use it in GitHub Desktop.
Save SebastienGicquel/f328df5282a0966d02c5445a9badcd05 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