Created
April 19, 2012 17:56
-
-
Save andho/2422621 to your computer and use it in GitHub Desktop.
Convert associative array into a query string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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