Create url from array php

How to serialize array as url queryString in php?

is there any built in function in php or i must create this my self?,expected output should require key name in input data array, see below and after that use http_build_query() function to created query string, I am trying to use a for loop in javascript to create a a table that displays * and for each row the columns increase by 1 * ,Online Free Tutorials Guruji Guide & Materials – Solved Questions Answers

Answer by Preston Savage

Generates a URL-encoded query string from the associative (or indexed) array provided. , May be an array or object containing properties. ,http_build_query — Generate URL-encoded query string, If data is an object, then only public properties will be incorporated into the result.

foo=bar&baz=boom&cow=milk&php=hypertext+processor foo=bar&baz=boom&cow=milk&php=hypertext+processor 

Answer by Creed Moreno

$data = [ 'foo' => 'bar', 'baz' => 'boom', 'cow' => 'milk', 'php' => 'hypertext processor' ]; echo http_build_query($data); // output: foo=bar&baz=boom&cow=milk&php=hypertext+processor echo http_build_query($data, '', '&'); // output: foo=bar&baz=boom&cow=milk&php=hypertext+processor

Answer by Guillermo Goodwin

PHP provides http_build_query() method to generate URL encoded query string. This is available with PHP 5 and later versions. The http_build_query() function converts an array to its equivalent URL-encoded query string.,Here, the ‘$vars’ is a multidimensional array contains employee data. When this will pass through http_build_query() method, it returns a complex url query string.,In the below example, we have used $vars array to create the URL query string. The $vars array contains data on search keyword and page number. In the next line, we have passed this array to the http_build_query() method to generate a query string.,The above code returns this url query string-

Читайте также:  Python in range включительно

The term ‘Query String‘ is the part of a Uniform Resource Locator(URL). This is in the form of a series of key-value pairs and called as url parameters. This is basically used to build a typical url or to get data from url. For example-

https://www.example.com/emp?name=Smith&dept=it&role=developer&exp=5

Syntax of http_build_query()

http_build_query($query, $numeric_prefix, $separator, $encoded_type)

In the below example, we have used $vars array to create the URL query string. The $vars array contains data on search keyword and page number. In the next line, we have passed this array to the http_build_query() method to generate a query string.

 23, 'search' => 'etutorialspoint'); $qs = http_build_query($vars); $url = 'http://www.example.com/search.php?' . $qs; echo $url; ?>

The above code returns this url query string —

http://www.example.com/search.php?page=23&search=etutorialspoint

Here, $vars is an indexed array containing employee data.

 '332'); $qs = http_build_query($vars); $url = 'http://www.example.com/search.php?' . $qs; echo $url; ?>

The above code returns this url query string-

http://www.example.com/search.php?0=employee&1=smith&2=technical&emp_id=332

Here, the ‘$vars‘ is a multidimensional array contains employee data. When this will pass through http_build_query() method, it returns a complex url query string.

array('name'=>'Smith', 'age'=>39, 'dept'=>'IT', 'dob'=>'9/22/1980'), 'role'=>array('developer', 'designer')); $qs = http_build_query($vars); $url = 'http://www.example.com/search.php?' . $qs; echo $url; ?>

The above script returns this url query string-

http://www.example.com/search.php?employee%5Bname%5D=Smith&employee%5Bage%5D=39&employee%5Bdept%5D=IT&employee%5Bdob%5D=9%2F22%2F1980&role%5B0%5D=developer&role%5B1%5D=designer

We can also use urlencode() function with serialize() function to create an URL-encoded string. In the given example, first we have converted the array into its byte stream representation, which is a string using serialize() function. And then, we have created an URL-encoded string of that byte stream.

 40, 'search' => 'etutorialspoint'); echo "http://www.example.com/search.php?" . urlencode(serialize($vars)); ?>
http://www.example.com/search.php?a%3A2%3A%7Bs%3A4%3A%22page%22%3Bi%3A40%3Bs%3A6%3A%22search%22%3Bs%3A15%3A%22etutorialspoint%22%3B%7D

Answer by Idris Rhodes

It looks beauty, but to use this approach be careful about url encoding .Look next:,$this->_postFields must be an string like a=2&b=t right? so if I want to send array of data with curl to another page I must turn array to query string right?,♦I tried serialize() and unserialize() but thier format is not same as query string right? so what should I do? (I need something like .serialize() in jQuery that work on array not FORM),Of course, after sending this $url we will not get expected variables from $_POST.

I have an array of data comes from $_POST and I want to send them by curl to another page.

 curl_setopt($s,CURLOPT_POST,true); curl_setopt($s,CURLOPT_POSTFIELDS,$this->_postFields); 

♦ And the destination path is not under my control and the $_POST in the destination should be as $_POST not as its base64 encoded so I can’t use such codes.

$array = array(1,2,3); $encoded = json_encode($array); $decoded = json_decode($encoded); print_r($decoded); 
curl_setopt($s, CURLOPT_POSTFIELDS, http_build_query($this->_postFields)); 

It looks beauty, but to use this approach be careful about url encoding .Look next:

$_POST["some value"]='value1'; // " " between $_POST["next value"]='value2'; // " " between $url = http_build_query($_POST); echo $url; // OUTPUT some+value=value1&next+value=value2 

Answer by Frida Goodman

To pass array by URL, we have to create URL-encoded query string of that array and then pass via URL. We can do it in two ways-,Step 1: Prepare the array to pass by URL,http_build_query() function can convert an array to its equivalent URL-encoded query string. See the following example-,How to Pass an Array as URL Parameter in PHP?

http_build_query() function can convert an array to its equivalent URL-encoded query string. See the following example-

'[email protected]', array("php","mysql"), 'age'=>28); echo "next page"; ?> 

Step 2: How to receive the array from URL:
In the page2.php page, to retrieve the values, use the array keys in the following way-

"; echo $_GET[0][0]; echo "
"; echo $_GET[0][1]; echo "
"; echo $_GET['age']; echo "
"; ?>

Step 1: Prepare the array to pass by URL
urlencode() function can also be used to create an URL-encoded string. But it can’t convert an array to an URL-encoded string. So, at first, we’ll convert the array into its byte stream representation which is a string using serialize() function, then we’ll create an URL-encoded string of that byte stream. See the code below-

'[email protected]', array("php","mysql"), 'age'=>28); echo "next page"; ?> 

Step 2: How to receive the array from URL
In the page2.php page, to retrieve the values from the URL, you’ve to reverse the previous processes.First decode it using urldecode() function, then, use unserialize() function in the following way-

"; echo $str_arr[0][0]; echo "
"; echo $str_arr[0][1]; echo "
"; echo $str_arr['age']; echo "
"; ?>

Answer by Ariel Campos

The http_build_query() will create a query string from an array or object. These strings can be appended to a URL to create a GET request, or used in a POST request with, for example, cURL., Build an URL-encoded query string from an array , Build an URL-encoded query string from an array ,http_build_query() will also work with multi-dimensional arrays:

The http_build_query() will create a query string from an array or object. These strings can be appended to a URL to create a GET request, or used in a POST request with, for example, cURL.

$parameters = array( 'parameter1' => 'foo', 'parameter2' => 'bar', ); $queryString = http_build_query($parameters); 

$queryString will have the following value:

parameter1=foo¶meter2=bar 

http_build_query() will also work with multi-dimensional arrays:

$parameters = array( "parameter3" => array( "sub1" => "foo", "sub2" => "bar", ), "parameter4" => "baz", ); $queryString = http_build_query($parameters); 

$queryString will have this value:

parameter3%5Bsub1%5D=foo¶meter3%5Bsub2%5D=bar¶meter4=baz 

which is the URL-encoded version of

parameter3[sub1]=foo¶meter3[sub2]=bar¶meter4=baz 

Answer by Jay McGuire

parse_str() Function: The parse_str() function is used to parse a query string into variables. The string passed to this function for parsing is in the format of a query string passed via a URL.,The parameters from a URL string can be be retrieved in PHP using pase_url() and parse_str() functions.,Approach: Parse the URL string using parse_url() function which will return an associative array that contains its (passed URL) various components. The query of the array returned by parse_url() function which contains a query string of URL.,Below examples uses parse_url() and parse_str() function to get the parameters from URL string.

parse_url( $url, $component = -1 )

Источник

How to Pass an Array in URL Query String with PHP

PHP includes the http_build_query() method for creating URL encoded query string. This method is available only on PHP 5 and above versions. In this snippet, you will learn how to pass an array in URL query string using PHP.

The syntax of http_build_query() will look as follows:

http_build_query($query, $numeric_prefix, $separator, $encoded_type)

Passing a Simple Array within http_build_query()

In this section, we are going to use $vars array for generating the URL query string. The $vars array includes the data of search keyword, as well as the page number.

Let’s see an example where this array is passed to the http_build_query() method for generating a query string:

 $vars = ['page' => 26, 'search' => 'w3docs']; $qs = http_build_query($vars); $url = 'http://www.example.com/search.php?' . $qs; echo $url; ?>

The code, represented above will return the following url query string:

http://www.example.com/search.php?page=26&search=w3docs

Passing an Indexed Array within http_build_query()

Now, let’s see an example where $vars is an indexed array that comprises an employee data:

 $vars = ['employee', 'brown', 'developer', 'emp_id' => '332']; $qs = http_build_query($vars); $url = 'http://www.example.com/search.php?' . $qs; echo $url; ?>

The code above will return the following url query string:

http://www.example.com/search.php?0=employee&1=brown&2=developer&emp_id=332

Passing a Multidimensional Array within http_build_query()

In this section, we will consider $vars as a multidimensional array with employee data. Once passing through the http_build_query() method, it will return a complex url query string.

 $vars = ['employee' => ['name' => 'Brown', 'age' => 41, 'dept' => 'IT', 'dob' => '9/22/1980'], 'role' => ['engineer', 'developer']]; $qs = http_build_query($vars); $url = 'http://www.example.com/search.php?' . $qs; echo $url; ?>

This code will return the following url query string:

http://www.example.com/search.php?employee%5Bname%5D=Brown&employee%5Bage%5D=39&employee%5Bdept%5D=IT&employee%5Bdob%5D=9%2F22%2F1980&role%5B0%5D=engineer&role%5B1%5D=developer

Describing Query String

The query string term is considered a part of Uniform Resource Locator (URL). It is in the form of a series of key-value pairs. Generally, it is used for creating a typical url or getting data from URL.

So, you can use the query string for different purposes such as search keywords, url building, specific web page tracking, and many more.

Источник

Оцените статью