1

I am working on a PHP controller, this is the URL:

www.example.com?field1=test1&field2=test2&field3=test3

I know I can get the value by this:

$_GET['field1'] // this will return test1

But I need something to return the name of the field, in this case field1, so I'm wondering if I can loop through the $_GET variable, but I'm not sure how.

Rodrigo Zurek
  • 4,555
  • 7
  • 33
  • 45

5 Answers5

4

$_POST is just an array. You can loop through it like this:

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

There's also an array_keys function that might be of use.

hanleyhansen
  • 6,304
  • 8
  • 37
  • 73
3

If you print_r(array_keys($_POST)) you can get an array containing all of the parameter names.

James
  • 5,137
  • 5
  • 40
  • 80
3

Use array_keys - "Return all the keys or a subset of the keys of an array"

http://us2.php.net/array_keys

Pat Butkiewicz
  • 663
  • 5
  • 9
2

To add to what @Marc B said, in php you can get the "keys" of an array with the array_keys method.

Here's a very nice url

wspurgin
  • 2,600
  • 2
  • 17
  • 20
0

The array used for url params is $_GET, and you could iterate the paramametters on this way

foreach($_GET as $key => $value) {
    printf ("%s => %s", $key, $value);
}

You could use any of the arrays function for work for parametters and their contents

Carlos
  • 4,299
  • 5
  • 22
  • 34