Is there a PHP equivalent to Java's ServletRequest.getParameterValues()? [duplicate] - php

This question already has answers here:
How to pass an array within a query string?
(12 answers)
Closed 4 years ago.
I have an array of parameter values (with matching parameter names) in my request URL, like this:
?my_array_of_values=First&my_array_of_values=Second
In a Java servlet, I can detect them like this:
ServletRequest request = ...;
String[] myArrayOfValues = request.getParameterValues("my_array_of_values");
And this will result in:
myArrayOfValues[0] = "First";
myArrayOfValues1 = "Second";
...which is what I want.
However, I am not sure how to get the same results in PHP. For the same (above) parameters, when I try:
print_r($_GET);
it results in
Array ( [my_array_of_values] => Second )
...i.e., the "First" parameter is lost.
I can see why this happens, but is there a way to detect such an array of parameter values in PHP as you can in Java/servlets? If not, is there a recommended workaround or alternative way of doing it?

I don't know about the strange magic that happens in a Java environment, but query params must have different names or else they will overwrite each other.
In the example of ?my_array_of_values=First&my_array_of_values=Second only the last given value is returned. It is the same as assigning different values to the same variable one after the other.
You may retrieve a single parameter as array though, by using angle brackets after the parameter name:
?my_array_of_values[]=First&my_array_of_values[]=Second
In that case $_GET['my_array_of_values'] will be an array with all the given values.
See also: Authoritative position of duplicate HTTP GET query keys

Related

Join strings and separate with a comma using PHP [duplicate]

This question already has answers here:
Implode all the properties of a given name in an array of object - PHP [duplicate]
(6 answers)
Closed 2 years ago.
Using PHP, I would like to select certain values from an array of objects, join them to form one continuous string separated by commas, and save this to a variable named $isbn.
I have an array of objects named $items. A var_dump($items) produces this. I need to select the item_isbn value.
My desired result is;
echo $isbn
// would produce
// '0-7515-3831-0,978-0-141-38206-7,978-1-30534-114-1'
Getting rid of the hyphens would be a bonus but I think I can achieve this using str_replace.
Here we go:
$isbnList = [];
foreach ($arrayObject as $item) {
if (isset($item->item_isbn)) {
$isbnList[] = $item->item_isbn;
}
}
$isbn = implode(",", $isbnList);
Check it:
echo $isbn;
For your information:
The foreach works because it's an array. It doesn't care each of the item inside it.
Each of the object ($item) in the loop is the default object of php (as the dump data you provided). Which is supposed to be called as $object->property. This object's property are not sure to always available, so checking whether it's available by php built-in function names isset() is essential.
By knowing implode() is built-in function from php to build a string with separator base on an one side array (means each of item in this one side array must be a scalar value (int, string)). We build this array to ready for the call.
The rest is just about syntax to how-to work with array or object... You can easily lookup on php manual page, eg: http://php.net/manual/en/language.types.array.php
Most popular programming languages nowadays provide us built-in function like this implode(), just different naming.

How to get hash(#) value in query string [duplicate]

This question already has answers here:
Get fragment (value after hash '#') from a URL [closed]
(10 answers)
Closed 6 years ago.
I am trying to post # parameter in query string like...
www.demo.php?tag=c#
Now i am trying to get tag value in another page with
$_GET['tag']
But i am not getting somehow # value is not coming with $_GET. I can get only "c".
The hash character (#) has a special meaning in URLs. It introduces a fragment identifier. A fragment identifier is usually used to locate a certain position inside the page.
A fragment identifier is handled by the browser, it is removed from the URL when the browser makes the HTTP request to the server.
Being a special character, if you want it to have its literal value you must encode it. The correct way to write the URL in your question is:
www.demo.php?tag=c%23
When it's written this way, $_GET['tag'] contains the correct value ('c#').
If you generate such URLs from PHP code (probably by getting the values of tag from an external resource like a database) you have to use the PHP function urlencode() to properly encode the values you put in URLs. Or, even better, create an array with all the URL arguments and their values then pass it to http_build_query() to generate the URL:
$params = array(
'tag' => 'c#',
'page' => 2,
);
$url = 'http://localhost/demo.php?'.http_build_query($params);

Extracting a URL string with same results as a $_GET [duplicate]

This question already has answers here:
Getting vars from URL
(6 answers)
Closed 8 years ago.
Ok, so I have a string like so:
index.php?page=test2;sa=blah
And I need only to grab what gets returned using $_GET['page'], so it will return, test2 in this case. BUT page MUST be defined DIRECTLY after index.php? before it can return an actual string. How do I do this? I just need the same results returned from a $_GET for the page variable. I can also have just this:
index.php?page=blah
Should return blah
or this:
index.php?page=anything&sa=blah
Returns anything
or this:
index.php?action=blah;page=2
Returns empty string, cause this is NOT directly after index.php?
or basically any url, but it needs to grab the variable of page if it exists, only after index.php?. And sometimes page might not even exist at all, in this case, an empty string should be returned.
How can I do this? I am not browsing to the URL in my browser, so, don't think $_GET will work, but it needs to simulate and return the EXACT same results that $_GET will return in the browser, but only if page is defined directly after index.php?
parse_url and parse_str are your friends.
Please use following code:
function get($name, $url) {
$src = parse_url($url);
$src = $src['query'];
parse_str($src, $tag);
return $tag[$name];
}
you can check if the element received at $_GET is equal that you expect by comparison or a simple if statement. if not, then process the string to find it (if needed). Maybe also used the index of the elemnt to determine if process or not.

Getting array param out of query string with PHP

(NOTE: This is a follow up to a previous question, How to pass an array within a query string?, where I asked about standard methods for passing arrays within query strings.)
I now have some PHP code that needs to consume the said query string- What kind of query string array formats does PHP recognize, and do I have to do anything special to retrieve the array?
The following doesn't seem to work:
Query string:
?formparts=[a,b,c]
PHP:
$myarray = $_GET["formparts"];
echo gettype($myarray)
result:
string
Your query string should rather look like this:
?formparts[]=a&formparts[]=b&formparts[]=c
If you're dealing with a query string, you are looking at the $_GET variable. This will contain everything after the ? in your previous question.
So what you will have to do is pretty much the opposite of the other question.
$products = array();
// ... Add some checking of $_GET to make sure it is sane
....
// then assign..
$products = explode(',', $_GET['pname']);
and so on for each variable. I must give you a full warning here, you MUST check what comes through the $_GET variable to make sure it is sane. Otherwise you risk having your site compromised.

conditional in_array trouble with PHP mySQL

I have firePHP so i know exactly what the variables are, but I can't figure out why this code doesn't change it.
I receive from a mySQL call $query (which if returned produces [{"type":"2"}])
I have 4 potential types, and things can be multiple types (i.e. [{"type":"1"},{"type":"2"}])
Now I want to read this data and run various other functions based on the type it has, that is: if it's only type 2, call function TWO, if it's type 1 and 2 call function ONE and function TWO. I thought this would be easiest if i moved all the data into another array.
Here is the code I currently have:
$result = array('message'=>false, 'money'=>false, 'glasses'=>false, 'exclamation'=>false);
if (in_array('1',$query)) {$result['message'] = true;}
if (in_array('2',$query)) {$result['money'] = true;}
if (in_array('3',$query)) {$result['glasses']=true;}
if (in_array('4',$query)) {$result['exclamation']=true;}
echo json_encode($result);
This however does not update the $result array (as I can tell all of the values of $message are false in firePHP.... Thus I assume something is wrong with my if statements, but what?
I´m not sure about the value of $query, but if it is something like:
array [0 => '{"type":"2"}']
You would have to use:
in_array('{"type":"2"}',$query)
as that is the value of your variable.
Is it because the results returned in $query are arrays of arrays, and thus in_array is only searching at the top level and not sub-levels? It seems like what you want is to recursively search $query.

Categories