Grabbing the name from data sent through post
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.
精彩评论