Read only parameter name in Querystring with $_GET - php

How can I read only the parameter name from a querystring in PHP? For example in both of these:
www.example.com/index.php?a='1'
www.example.com/index.php?a
the wanted output is:
a

You can use the array_keys of the global $_GET array:
$keys = array_keys($_GET);
echo $keys[0]; // echos a

You need get a key of a GET:
foreach ($_GET as $key => $val)
{
print($key);
}
Or a get via print_r(array_keys($_GET)); function.

$_GET is just an array variable (not command). It is populated with parameter names as keys in this array, and parameter values as the corresponding values.
So just use array_keys($_GET).

Related

Get names of GET method variables

I have about 5 variables in GET method. They almost always have different names, encoded mostly. How i can get name (not value) of those variables.
example:
$_GET['orchid'] = red;
$_GET['xyc'] = wrack;
and after that, next time i open the page:
$_GET['rose'] = red;
$_GET['gzuy'] = bottle;
Values are not important for now, in this case I need names of variables: "orchid", "xyc" or in second case "rose" and "gzuy".
array_keys($_GET)
For more informations, see the link bellow:
http://php.net/manual/function.array-keys.php
foreach ($_GET as $key=>$value){
echo $key;
}
array_keys() should do the trick:
$keys = array_keys($_GET);
foreach ($_GET as $key => $value) {
//Line below is optional to get around empty values.
if (!empty($value))
echo $key, ' ';
}
The above code will print out all of the set $_GET variables, having file.php?moo will mark moo as set but with a value of nothing. The below snippet will simply return an array just containing the names of the $_GET variables which can then be used in $_GET[$keys[0]] for example to recall its value.
array_keys($_GET);
Docs:
foreach loop
array_keys()

Assign GET variable to another in PHP when you know part of the name of the GET variable

I have some problems with GET variable in PHP.
I need to assign a GET variable to a normal variable in PHP, but the name of the GET variable is not always the same name.
Ex. $telf=$_GET['t_1'].
But can be $_GET['t_6'], $_GET['t_18'].
There's any way to assign all possible names of $_GET['t_XX'] to another variable?
You could use preg_grep.
With preg_grep you can get all keys in $_GET that start with 't_'. Then you walk over these values, and print (or do whatever you want) with the key + value.
Please see this example:
$keys = preg_grep("/^t_.*/", array_keys($_GET));
foreach($keys as $key) {
echo $key.' holds value '.$_GET[$key].PHP_EOL;
}
Another option a bit lighter in resources is using strpos on the keys
foreach ( $_GET as $keys => $values ) {
if ( strpos( $keys, 't_' ) !== false ) {
$final_var = $values;
}
}

php string manipulation received by url after installing facebook app

I get this string from URL after installing a facebook page app on several pages.
tabs_added[255408179646]=1&tabs_added[197573531148]=1&tabs_added[225556742602]=1&tabs_added[201931451540]=1&tabs_added[205657687417]=1
I am looking for a way to itterate and be able to loop and echo each numeric value within brackets [].
The values are stored in $_GET['tabs_added'], and you can access them like this:
$tabs_added = $_GET['tabs_added'];
foreach($tabs_added as $key => $value){
echo $key;
}
You should probably check if the values are present with if(isset($_GET['tabs_added'])) first.
The array should be stored in $_GET['tabs_added']. So you can try something like this to loop through it;
foreach($_GET['tabs_added'] AS $key => $value)
{
echo $key;
}
parse_str is made for parsing query strings.
parse_str('tabs_added[255408179646]=1&tabs_added[197573531148]=1', $parsed);
var_dump($parsed);
You can get an array of tabs through either the $_GET or $_POST superglobal variable, depending on whether this is a GET or POST request.
$tabs = $_GET["tabs_added"]; // or $_POST['tabs_added'];
foreach($tabs as $k => $v) {
echo $k;
}
You can also check if the param is present first to avoid an Undefined Index Notice for $_GET["tabs_added"]; and subsequent Invalid argument Warning passing NULL to the foreach loop.
isset($_GET['tabs_added']

Can't use $_GET as a numerical indexed array

i am at prototype stage. I have a link in page1.php that sends to page below:
http://localhost/sayfa.php?rd_dil=turkish&rd_sayfa=yazilar&rd_yazar=ali_uysal&rd_baslik=kalem_ucu"
in this page, echo $_GET['rd_dil'] works and displays turkish but echo $_GET[0] displays a Notice : Undefined offset: 0
so I want to work with $_GET in numerical way (numerical index) ? how can I achieve this aim? I read php.net + stack overflow and googled but I couldn't solve my issue.
$_GET is an assoziative array, to loop over it:
foreach($_GET as $key=>$value) {
....
}
In case you want only the values in a numeric array, you could use:
$myData = array_values($_GET);
// here you have a numeric array containing the $_GET values
echo $myData[0];
Since $_GET is an associate array, you can assign the values to a new array:
foreach($_GET as $key=>$val) {
$_GET2[] = $val;
}
Or you can use array_values as suggested by axel.michel:
$_GET2 = array_values($_GET);
echo $_GET2[0];
You can’t do that directly. But there are some workarounds:
$indexed = array_values($_GET);
$first = $indexed[0];
$keys = array_keys($_GET);
$first = $_GET[$keys[0]];
$first = current(array_slice(array('foo'), 0, 1)));
Yes, you can't. That's just how it works.
There is just no such index.
You don't need numerical indexes though, but have to use associative keys.
There are 2 reasons why you shouldn't translate your $_GET into enumerated list:
parameter order is not guaranteed. you have to use field names instead of positions.
it's just useless waste of CPU. Everything you want from your enumerated array, you can get from original $_GET. Use foreach() to iterate it for example.
If you still don't know how to handle $_GET properly - ask this very question, and you will get the proper answer.

Can items in PHP associative arrays not be accessed numerically (i.e. by index)?

I'm trying to understand why, on my page with a query string,
the code:
echo "Item count = " . count($_GET);
echo "First item = " . $_GET[0];
Results in:
Item count = 3
First item =
Are PHP associative arrays distinct from numeric arrays, so that their items cannot be accessed by index? Thanks-
They can not. When you subscript a value by its key/index, it must match exactly.
If you really wanted to use numeric keys, you could use array_values() on $_GET, but you will lose all the information about the keys. You could also use array_keys() to get the keys with numerical indexes.
Alternatively, as Phil mentions, you can reset() the internal pointer to get the first. You can also get the last with end(). You can also pop or shift with array_pop() and array_shift(), both which will return the value once the array is modified.
Yes, the key of an array element is either an integer (must not be starting with 0) or an associative key, not both.
You can access the items either with a loop like this:
foreach ($_GET as $key => $value) {
}
Or get the values as an numerical array starting with key 0 with the array_values() function or get the first value with reset().
You can do it this way:
$keys = array_keys($_GET);
echo "First item = " . $_GET[$keys[0]];
Nope, it is not possible.
Try this:
file.php?foo=bar
file.php contents:
<?php
print_r($_GET);
?>
You get
Array
(
[foo] => bar
)
If you want to access the element at 0, try file.php?0=foobar.
You can also use a foreach or for loop and simply break after the first element (or whatever element you happen to want to reach):
foreach($_GET as $value){
echo($value);
break;
}
Nope -- they are mapped by key value pairs. You can iterate the they KV pair into an indexed array though:
foreach($_GET as $key => $value) {
$getArray[] = $value;
}
You can now access the values by index within $getArray.
As another weird workaround, you can access the very first element using:
print $_GET[key($_GET)];
This utilizes the internal array pointer, like reset/end/current(), could be useful in an each() loop.

Categories