Php array to query params

PHP Build url query from array

(you get the idea) I’m trying to use http_build_query($serials, ‘serial’, ‘&’); but it adds the numeric index to the prefix ‘serial’. Any idea how to remove that numeric index?

7 Answers 7

Well you can’t really have the same GET parameter in the string, After all if you try to access that variable server side, what would you use?

$_GET[‘serial’] — But which serial would it get?

If you really want to get a list of serials, Simply turn the array into a string, save it as an array and there you go. for example :

$serials = «string of serials, delimited by &» ;
Then you can use the http build query.

Yes, But that means your node.JS API is a bit wrong, This isn’t a correct way of passing arguments in as GET parameters.

$get = "localhost/get?serial=" . $serials[0]; unset( $serials[0] ); foreach( $serials AS serial ) < $get . mt24">
)" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">Share
)" title="">Improve this answer
)">edited Nov 19, 2016 at 13:54
SharpC
6,9264 gold badges45 silver badges40 bronze badges
answered Sep 20, 2014 at 8:48
3
    1
    That will get you something like localhost/get??serial=1?serial=2?serial=3?serial=4. .
    – Mario
    Sep 20, 2014 at 8:49
    Thanks man! I've made few adjustments but you point me to the right direction :)
    – ranslobo
    Sep 20, 2014 at 9:02
    Isnt that an inefficient solution. A simple implode would have worked. And moreover having same get variable isn't going to solve any purpose so it should not be encouraged
    – nitigyan
    Sep 20, 2014 at 10:43
Add a comment|
2

Just as an FYI, PHP doesn't handle multiple GET variables with the same name natively. You will have to implement something fairly custom. If you are wanting to create a query string with multiple serial numbers, use a delimiter like _ or -.

Ex: soemthing.com/serials.php?serials=09830-20990-91234-12342

To do something like this from an array would be simple

$get_uri = "?serial=" . implode("-", $serials);

You would be able to get the array back from a the string using an explode to

$serials = explode("-", $_GET['serials']); 

Yes its quite possible to have such format, you have to build it query string by indices. Like this:

No need to build the query string by hand, use http_build_query() in this case:

$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689']; $temp = $serials; unset($serials); $serials['serial'] = $temp; $query_string = http_build_query($serials); echo urldecode($query_string); // serial[0]=3804689&serial[1]=3801239&serial[2]=3555689&serial[3]=3804687&serial[4]=1404689&serial[5]=6804689&serial[6]=8844689&serial[7]=4104689&serial[8]=2704689&serial[9]=4604689 

And then finally, if you need to process it somewhere, just access it thru $_GET['serial'];

$serials = $_GET['serial']; // this will now hold an array of serials 

@BayssMekanique yes this is quite possible, instead of naming each individual data with other names, they could be gathered with the same name, so that when you access that query name, they will hold the list of values that you set

$get = "localhost/get?serial="; $serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689']; $list = implode("&serial mt24"> 
)" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">Share
)" title="">Improve this answer
answered Sep 20, 2014 at 9:07
Add a comment |
0

Having the same GET parameter will not work this way. You will need to use an array in the get parameter:

localhost/get?serial[]=3804689&serial[]=3801239&serial[]=3555689 

IF you just print $_GET through this URL, you will receive

Array ( [serial] => Array ( [0] => 380468 [1] => 3801239 [2]=> 3555689) ) 

Then you can do this to build your query string

Источник

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.

Источник

How to pass an array in a URL query string using PHP

In this article, you will learn how to pass an array from one page to another in a URL query string using PHP.

You will learn not only how to pass a single array, but also learn to pass indexed and multidimensional arrays to an URL query string using PHP.

The term 'Query String' is a part of a Uniform Resource Locator(URL). This is in the form of a series of key-value pairs and is called as url parameter. 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

In the above url, 'name=Smith&dept=it&role=developer&exp=5' is the query string, having multiple parameters. The parameters are separated by '&' and within each pair, the field name and value are separated by an equal sign(=).

The query string is used for many purposes, like the form get method, which takes input field values in the url query string, search keywords, particular web page tracking, url building, and so on.

In HTML, you have observed how the form input name with value is passed in a URL query string if the form method is post. But, this generates only a static query string. To build query strings dynamically, methods are provided by almost all popular programming languages. In this article, you will learn how to pass an array variables in a URL query string using PHP.

How to pass array in URL query string using PHP

PHP pass an array in a URL query string using http_build_query() function

PHP provides the 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.

Syntax of http_build_query()

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

$query- It is an associative array or indexed array containing parameters. This can be a one-dimensional array or a multidimensional array.
$numeric_prefix- A numeric prefix is provided if numeric indices are used in the base array.
$separator- This overrides the default parameter separator.
$encoded_type- This is the query string encoding type, like- PHP_QUERY_RFC1738, PHP_QUERY_RFC3986.

Pass simple array in http_build_query()

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

?php $vars = array('page' => 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

Pass indexed array in http_build_query()

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

?php $vars = array('employee','smith','technical','emp_id' => '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

Pass multidimentional array in http_build_query()

Here, the '$vars' is a multidimensional array containing employee data. This is returned by the http_build_query() method as a complex url query string.

?php $vars = array('employee'=>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

PHP pass an array in a URL query string using serialize() and urlencode() functions

We can also use the urlencode() function with the 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 the serialize() function. And then, we have created an URL-encoded string of that byte stream.

?php $vars = array('page' => 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

Источник

Читайте также:  Подсчет элементов массива javascript
Оцените статью