We often need to convert a PHP array to a query string for use in a URL or POST request, and vice versa. Really, PHP is very rich with its build in functions as it has very useful function http_build_query() to convert a PHP array to a query string. Here in this post, I have an example to convert a PHP array to query string.
Also, read:
- Convert Associative Array into XML in PHP
- Convert XML into Associative Array in PHP
- Converting an Array to JSON in PHP
- Converting JSON to Array or Object in PHP
Generate a URL query string from a PHP array
First, let us take a look at the following PHP array:
<?php $myarray= array( 'apikey'=>‘xg6tr7k’, 'user'=>'abcd', 'email'=>'jhon.php@example.com' ); ?>
Now let’s take a look at how we can convert above array to a query string using our PHP function http_build_query():
<?php echo http_build_query($myarray); ?>
OUTPUT:
apikey=xg6tr7k&user=abcd&email=jhon.php%40example.com
You may also like:
- Working with php.ini file Configuration
- Control Statements in PHP
- Convert Associative Array into XML in PHP
- Convert XML into Associative Array in PHP
- Using Prepared Statement with PHP & MySQL
- How to Upload File in PHP
- Converting an Array to JSON in PHP
- Converting JSON to Array or Object in PHP
- Manipulating PHP arrays: push, pop, shift, unshift
- Remove Repeated Words From String in PHP
- Converting a PHP Array to a Query String
- 15+ regular expressions for PHP developers
- 10 Most Important Directory Functions in PHP
- 10 little known but useful PHP functions
- PHP Script to Download Large Files Reliably