Can I have multiple $_GET with the same key, different values?
Yes, use user[] as key. should work. PHP access all $_POST[] variables into an array?
Can I have multiple $_GET with the different key, same values?
I did not found any solution using only PHP so the JavaScript help me to make it done. Check the following snippets.
JavaScript
function changeText(containerId) {
var datatext = document.getElementById('omdbBox').value;
var collection = document.getElementById(containerId).getElementsByTagName('INPUT');
for (var x = 0; x < collection.length; x++) {
if (collection[x].type.toUpperCase() == 'TEXT') collection[x].value = datatext;
}
}
CSS
#cseBox {display: none;}
PHP (to print the submitted URL, you don't have to use this php code while using it, it is just for test purpose. It will make sure that code is working.)
<?php
if(isset($_GET['search'])){
echo 'http://moviesnight.club/?<b>t='.$_GET['t'].'&q='.$_GET['q'].'</b>&search=';
}
?>
HTML
<form method="get">
<input id="omdbBox" type="text" autocomplete="off" name="t" onkeyup="changeText('cseBox')">
<div id="cseBox">
<input type="text" name="q">
</div>
<input type="submit" name="search">
</form>
Also check the live PhpFiddle
Correct way to pass multiple values for same parameter name in GET request
Indeed, there is no defined standard. To support that information, have a look at wikipedia, in the Query String chapter. There is the following comment:
While there is no definitive standard, most web frameworks allow
multiple values to be associated with a single field.[3][4]
Furthermore, when you take a look at the RFC 3986, in section 3.4 Query, there is no definition for parameters with multiple values.
Most applications use the first option you have shown: http://server/action?id=a&id=b
. To support that information, take a look at this Stackoverflow link, and this MSDN link regarding ASP.NET applications, which use the same standard for parameters with multiple values.
However, since you are developing the APIs, I suggest you to do what is the easiest for you, since the caller of the API will not have much trouble creating the query string.
HashMap with multiple values under the same key
You could:
- Use a map that has a list as the value.
Map<KeyType, List<ValueType>>
. - Create a new wrapper class and place instances of this wrapper in the map.
Map<KeyType, WrapperType>
. - Use a tuple like class (saves creating lots of wrappers).
Map<KeyType, Tuple<Value1Type, Value2Type>>
. - Use mulitple maps side-by-side.
Examples
1. Map with list as the value
// create our map
Map<String, List<Person>> peopleByForename = new HashMap<>();
// populate it
List<Person> people = new ArrayList<>();
people.add(new Person("Bob Smith"));
people.add(new Person("Bob Jones"));
peopleByForename.put("Bob", people);
// read from it
List<Person> bobs = peopleByForename["Bob"];
Person bob1 = bobs[0];
Person bob2 = bobs[1];
The disadvantage with this approach is that the list is not bound to exactly two values.
2. Using wrapper class
// define our wrapper
class Wrapper {
public Wrapper(Person person1, Person person2) {
this.person1 = person1;
this.person2 = person2;
}
public Person getPerson1() { return this.person1; }
public Person getPerson2() { return this.person2; }
private Person person1;
private Person person2;
}
// create our map
Map<String, Wrapper> peopleByForename = new HashMap<>();
// populate it
peopleByForename.put("Bob", new Wrapper(new Person("Bob Smith"),
new Person("Bob Jones"));
// read from it
Wrapper bobs = peopleByForename.get("Bob");
Person bob1 = bobs.getPerson1();
Person bob2 = bobs.getPerson2();
The disadvantage to this approach is that you have to write a lot of boiler-plate code for all of these very simple container classes.
3. Using a tuple
// you'll have to write or download a Tuple class in Java, (.NET ships with one)
// create our map
Map<String, Tuple2<Person, Person> peopleByForename = new HashMap<>();
// populate it
peopleByForename.put("Bob", new Tuple2(new Person("Bob Smith",
new Person("Bob Jones"));
// read from it
Tuple<Person, Person> bobs = peopleByForename["Bob"];
Person bob1 = bobs.Item1;
Person bob2 = bobs.Item2;
This is the best solution in my opinion.
4. Multiple maps
// create our maps
Map<String, Person> firstPersonByForename = new HashMap<>();
Map<String, Person> secondPersonByForename = new HashMap<>();
// populate them
firstPersonByForename.put("Bob", new Person("Bob Smith"));
secondPersonByForename.put("Bob", new Person("Bob Jones"));
// read from them
Person bob1 = firstPersonByForename["Bob"];
Person bob2 = secondPersonByForename["Bob"];
The disadvantage of this solution is that it's not obvious that the two maps are related, a programmatic error could see the two maps get out of sync.
how to post multiple value with same key in python requests?
Dictionary keys must be unique, you can't repeat them. You'd use a sequence of key-value tuples instead, and pass this to data
:
requests.post(url, data=[('interests', 'football'), ('interests', 'basketball')])
Alternatively, make the values of the data
dictionary lists; each value in the list is used as a separate parameter entry:
requests.post(url, data={'interests': ['football', 'basketball']})
Demo POST to http://httpbin.org:
>>> import requests
>>> url = 'http://httpbin.org/post'
>>> r = requests.post(url, data=[('interests', 'football'), ('interests', 'basketball')])
>>> r.request.body
'interests=football&interests=basketball'
>>> r.json()['form']
{u'interests': [u'football', u'basketball']}
>>> r = requests.post(url, data={'interests': ['football', 'basketball']})
>>> r.request.body
'interests=football&interests=basketball'
>>> r.json()['form']
{u'interests': [u'football', u'basketball']}
How to get multiple parameters with same name from a URL in PHP
Something like:
$query = explode('&', $_SERVER['QUERY_STRING']);
$params = array();
foreach( $query as $param )
{
// prevent notice on explode() if $param has no '='
if (strpos($param, '=') === false) $param += '=';
list($name, $value) = explode('=', $param, 2);
$params[urldecode($name)][] = urldecode($value);
}
gives you:
array(
'ctx_ver' => array('Z39.88-2004'),
'rft_id' => array('info:oclcnum/1903126', 'http://www.biodiversitylibrary.org/bibliography/4323'),
'rft_val_fmt' => array('info:ofi/fmt:kev:mtx:book'),
'rft.genre' => array('book'),
'rft.btitle' => array('At last: a Christmas in the West Indies.'),
'rft.place' => array('London'),
'rft.pub' => array('Macmillan and co.'),
'rft.aufirst' => array('Charles'),
'rft.aulast' => array('Kingsley'),
'rft.au' => array('Kingsley, Charles'),
'rft.pages' => array('1-352'),
'rft.tpages' => array('352'),
'rft.date' => array('1871')
)
Since it's always possible that one URL parameter is repeated, it's better to always have arrays, instead of only for those parameters where you anticipate them.
Get values of same key from multiple array in php
You are searching for array_column.
Here is the syntax
array array_column ( array $input , mixed $column_key [, mixed $index_key = null ] )
Description
array_column — Return the values from a single column in the input array
Example :
$records = array(
array(
tid => 167
),
array(
'id' => 3245,
'first_name' => 'Sally',
tid => 166
),
array(
'id' => 5342,
'first_name' => 'Jane',
tid => 168
),
array(
'id' => 5623,
'first_name' => 'Peter',
tid => 169
)
);
$ids= array_column($records, 'tid');
OUTPUT :
Array
(
[0] => 167
[1] => 166
[2] => 168
[3] => 169
)
If you have more arrays,
$records1 = [ ['tid' => 169]];
$ids1 = array_column($records1, 'tid');
then you can do array_merge.
$ids = array_merge($ids, $ids1);
OUTPUT :
Array
(
[0] => 167
[1] => 166
[2] => 168
[3] => 169
[4] => 169
)
$_POST key with multiple values for checkbox options
Appending the params to the generated query string by http_build_query
did the trick.
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)."&colbits=cb_id&colbits=cb_altid&colbits=cb_ra&colbits=cb_mag",
),
);
Related Topics
PHP Preg-Replace More Than One Underscore
Getting All $_Post from Multiple Select Value
Find and Replace Keywords by Hyperlinks in an HTML Fragment, via PHP Dom
Prevent Browser Back Button Cache
How to Render Content from String/Database with Twig
Sparql Query to Get All Parent of a Node
How to Get a List of Databases
PHP Regex Get Text Between Bbcode Tags
Is There a Difference Between Instantiation with Parentheses or Without
How to Get an Option Previously Set with Curl_Setopt()
How to Pass Variables Between PHP Scripts
Incrementing Numbers Starting from 0000 in PHP
How to Put a Translation System in PHP Website
Strtolower() for Unicode/Multibyte Strings
PHP Upload Size and Its Impact on Post Size and Memory Limit