Grabbing the name from data sent through post - php

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.

Related

Creating an associative array from an array, based on the array - PHP

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.

getting all the keys for a map such as $_POST

For example, say I post some data to a php file, but I don't know what the names of those values are. Where I would normally perform $_POST["username"] or something similar. How would I go about getting a list of all the key/value pairs within $_POST
array_keys($_POST) will give you the array keys.
You can also do this to get values with key names:
foreach ($_POST as $key => $value)
{
//do stuff;
}
However!!! Why wouldn't you know what keys are in the post? You don't want hackers putting random stuff into a post, sending it to you, and processing away. There is nothing preventing them from putting in 1000s of entries.
Use array_keys to obtain all keys in $_POST super global array:
array_keys($_POST)
Simple example:
foreach (array_keys($_POST) as $key)
{
print $_POST[$key];
}

looping through a session array

Hello i got a question, i have a session array called 'addToCart', in there are multiple arrays with various id's, i would like to loop through these arrays with a foreach regardless of what the id name is. Does anyone know how i would approach this?
Does the following construction satisfy your needs?
foreach ($addToChart as $key=>$value){
// do anything you want with $key and $value
}
If you need to check "sub-arrays", you can check $value with is_array() function and add one more foreach loop inside.

multidimensional array

Please explain what is the meaning of
foreach ($toplist['children'] as $subkey => $subname)
and where the children come from. I'm confused.
Basically $toplist is an array of values. One of those values has been called 'children'.
In this case, the value at position 'children' is itself an array.
Your line of code is telling the computer to loop over each of the values inside the 'children' array and extract the key and value.
$subkey is the key, $subname is the name.
In other words, $toplist['children'][$subkey] == $subvalue
The other elements are coming from the $toplist['children'] array which you got to figure out where it is coming from since you have not put in all the needed code for the question. See this about foreach machenism to learn more about it.
his simply gives an easy way to
iterate over arrays. foreach works
only on arrays, and will issue an
error when you try to use it on a
variable with a different data type or
an uninitialized variable.
php.net

Getting the Get Variable's Name

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
}

Categories