Traverse $ _POST Array to display fields. Names

Is there a way to traverse an array, such as $ _POST, to see field names, not just values. To see the meanings, I am doing something like this.

foreach ($_POST as $value){ echo $value; } 

This will show me the values ​​- but I would also like to display the names in this array. If my $ _POST value was something like $ _POST ['something'] and it was stored 55; I want to output "something."

I have several input fields for which I need this.

+4
source share
5 answers

Do you mean this?

 foreach ( $_POST as $key => $value ) { echo "$key : $value <br>"; } 

you can also use array_keys if you want the key array to be array_keys over.

You can also use array_walk if you want to use the callback to iterate:

 function test_walk( &$value, $key ) { ...do stuff... } array_walk( $arr, 'test_walk' ); 
+8
source
 foreach ($_POST as $key => $value) { echo $key; // Field name } 

Or use array_keys to extract all keys from an array.

+2
source
 foreach ($_POST as $key => $value){ echo $key.': '.$value.'<br />'; } 
+2
source

If you just need the keys:

 foreach (array_keys($_POST) as $key) { echo $key; } 

Or...

 foreach ($_POST as $key => $value) { echo $key; } 

If you need both keys and values:

 foreach ($_POST as $key => $value) { echo $key, ': ', $value; } 
+1
source

Keys only:

$ array = array_keys ($ _ POST);

Print them with:

var_dump ($ array);

-or -

print_r ($ array);

+1
source

All Articles