Guyver
Guyver

Reputation: 165

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?

Upvotes: 0

Views: 66

Answers (3)

Felix Kling
Felix Kling

Reputation: 816462

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.

Upvotes: 0

Cameron Skinner
Cameron Skinner

Reputation: 54316

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
}

Upvotes: 0

bharath
bharath

Reputation: 1233

you need

print_r(array_keys($_POST));

check this for more details http://php.net/manual/en/function.array-keys.php

Upvotes: 2

Related Questions