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.
Generate a URL query string from a PHP array
First let us take a look at the following PHP array:
Now let’s take a look at how we can convert above array to a query string using our PHP function http_build_query():
OUTPUT:
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