Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Traverse $_POST Array to show field Names

Tags:

php

http-post

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.

like image 523
Haru Avatar asked Dec 10 '22 06:12

Haru


2 Answers

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' );
like image 101
zzzzBov Avatar answered Dec 11 '22 20:12

zzzzBov


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

Or use array_keys to fetch all the keys from an array.

like image 21
Luwe Avatar answered Dec 11 '22 18:12

Luwe