Is there a way to traverse an array such as $_POST to see the field names and not just the values. To see the values I do something like this.
foreach ($_POST as $value){
echo $value;
}
This will show me the values - but I would like to also display the names in that array. If my $_POST value was something like $_POST['something'] and it stored 55; I want to output "something".
I have a few select fields that I need this for.
You mean like this?
foreach ( $_POST as $key => $value )
{
echo "$key : $value <br>";
}
you can also use array_keys
if you just want an array of the keys to iterate over.
You can also use array_walk
if you want to use a callback to iterate:
function test_walk( &$value, $key )
{
...do stuff...
}
array_walk( $arr, 'test_walk' );
foreach ($_POST as $key => $value) {
echo $key; // Field name
}
Or use array_keys to fetch all the keys from an array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With