简体   繁体   中英

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

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM