i have a need to get an undefined $_GET['$VARIABLE'].
so that if a link comes into item.php?changingitemstyle=10101010101010
I need to find out how to extract the name of the variable not the value.
Any ideas?
Use the array_keys() function to retrieve an array of the keys of an array.
figured it out
php function array_keys
$_GET is a dictionary, so you can get the keys easily enough (unless I'm misunderstanding the question):
foreach( $_GET as $var_name => $value ) {
// Do something here
}
Related
Look I have a form like this:
<form method="post">
<input name="variable[var1][var2]" value="44"/>
</form>
I want to get the value of variable[var1][var2] in PHP using a string like:
$str = "['variable']['var1']['var2']";
echo "{$_POST{$str}}";
Why I need to do that?
I need it because the code that gets the value is totally dynamic and I cannot get or set manually the value using $_POST['variable']['var1']['var2'].
Can you help?
NOTE: I know this is possible to get it using $_POST['variable']['var1']['var2'], please don't ask me about why I'm not using this way. Simply I need to use as above (using $str).
You can use preg_match_all() to get all the indexes in the string into an array:
$indexes = preg_match_all('/(?<=\[\')(?:[^\']*)(?=\'\])/', $str);
$indexes = $indexes[0]; // Get just the whole matches
Then use a loop to drill into $_POST.
$cur = $_POST;
foreach ($indexes as $index) {
$cur = $cur[$index];
}
At this point $cur will contain the value you want.
You're WAY overthinking it. Anything you put in [] array notation in a form field's name will just become an array key. The following is LITERALLY all you need:
<input name="foo[bar][baz][qux]" ... />
$val = $_POST['foo']['bar']['baz']['qux'];
You cannot use a string as an "address" into the array, not without insanely ugly hacks like eval, or parsing the string and doing a loop to dig into the array.
It's hard to believe that this is a requirement. If you could expand more on what you're trying to achieve, someone undoubtedly has a better solution. However, I will ignore the eval = evil haters.
To echo:
eval("echo \$_POST$str;");
To assign to a variable:
eval("\$result = \$_POST$str;");
If you're open to another syntax then check How to write getter/setter to access multi-level array by key names?
In my code, I fill an array using this line in a loop :
$_SESSION['my_array'][] = $some_value;
After each execution of this line, I do some check (doesn't matter here for which purpose) using the function in_array(). However, at the first iteration it says :
« in_array() expects parameter 2 to be array ».
How to fix this problem?
You can initialize the array (before you fill it with values) like this:
$_SESSION['my_array']=array();
This way you can be sure that it is array, even when it would be empty.
You are assigning or accesing it wrongly
Use this
$_SESSION['my_array'] = $some_value;
When you are doing the in_array check, you can cast the second item to an array, so if it's empty then it will pass an empty array. This way you don't ever set anything to the session when you don't need to (which could trip you up later on)
e.g.,
if (in_array('foo', (array)$_SESSION['my_array'])) {
// do something
}
I have a unique issue that I have never heard of this being possible to do before:
So essentially I have a function that takes in a an array of arguments such as:
function someFunction(array $arguments){}
and gives me an array back as such:
array('option1', 'option2', 'options3', ...);
I then need to take that array and loop through it creating an associative array such as:
array('option1' => call_come_method('option1'), .... );
heres the kicker, you will never know how many arguments a user passes into the function, yet each one needs to created into a key=>value arrangement as seen above.
Now i did some research and I was told the $argv command in php, how ever where I am stumped is how to implement it in this case.
So if any one can give me any pointers I would be appreciative.
This is a lot easier than you think. First use array_flip to switch the array's keys and values.
$newArray = array_flip($arguments);
Then loop though it and call the method:
foreach($newArray as $key=>&$val){
$val = call_come_method($key);
}
The &, makes it a reference, so the array value is updated.
DEMO: http://codepad.org/giL1KPA3
UPDATE: You don't even need array_flip, you just need a for loop.
$newArray = array();
foreach($arguments as $val){
$newArray[$val] = call_come_method($val);
}
DEMO: http://codepad.org/AQ1gWrou
you will never know how many arguments a user passes into the function
FYI, you get to know it with func_get_args().
This way you don't need to provide your function with an $arguments parameter, but you just leave it empty.
I'm curious, more than anything - is it possible to get a PHP value posted? i.e. $_POST['foo'] via some kind of indexing? i.e. $_POST[0]?
($_POST[0] does not work by the way)
No, it's not possible: you cannot fetch values from an associative array by numeric indexes (because, as clearly noted in the doc, PHP does not distinguish between indexed and associative arrays).
That's why some functions (PDOStatement::fetch and its siblings, for example) that return arrays take an additional param to control the 'type' of indexes in the array returned: numeric (FETCH_NUM), string (FETCH_ASSOC) or both (FETCH_BOTH, the default value). )
The closest you might get with reindexing:
$myPost = array_values($_POST);
not that i'm aware of, check out a print_r($_POST) to see all goodies that you can access. You could iterate over the values with:
foreach($_POST as $key=>$value){
echo $key.' '.$value."\n";
}
You could throw in an $i++ if you wanted to keep track of a count ....
You can take data stored in $_POST variables and store them into indexed elements. But they are not stored as an indexed element initially.
When I send over post data I do a print_r($_POST); and I get something like this...
Array ( [gp1] => 9 )
Is there a way to get the "gp1", the name sent over as a value? I tried doing.
echo key($_POST["gp1"]);
But no luck there, I figured it would echo gp1. Is there a way to do this?
you need
print_r(array_keys($_POST));
check this for more details http://php.net/manual/en/function.array-keys.php
You could use foreach to see each key-value pair, or use array_keys to get a list of all keys.
foreach ($_POST as $key => $value) {
// Do whatever
}
Well, if you can write $_POST["gp1"] you already have the key anyway ;)
key() works differently, it takes an array as argument:
The key() function simply returns the key of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, key() returns NULL.
So if you have not done anything with the array (no traversing), key($_POST) would give you the key of the first element of the array.
Maybe you want a foreach loop?
foreach($_POST as $key => $value) {
}
There are other methods to retrieve keys as to well. It depends on what you want to do.