Rodrigo Zurek
Rodrigo Zurek

Reputation: 4575

Get URL parameter name in PHP

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.

Upvotes: 1

Views: 223

Answers (5)

Carlos
Carlos

Reputation: 4637

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

Upvotes: 0

Pat Butkiewicz
Pat Butkiewicz

Reputation: 683

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

https://www.php.net/array_keys

Upvotes: 3

wspurgin
wspurgin

Reputation: 2733

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

Upvotes: 2

James
James

Reputation: 5169

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

Upvotes: 3

hanleyhansen
hanleyhansen

Reputation: 6452

$_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.

Upvotes: 4

Related Questions