5

I want to save the name of all the $_GET variables in a url, but im not sure where to start, or finish for that matter.

For example:

if i have:

url: test.com/forums.php?topic=blog&discussion_id=12

can i use php to get the name, i.e. "topic" and "discussion_id from the $_GET variables and can i then store the values: "topic" and "discussion_id" in an array?

5 Answers 5

13

You can get this by calling array_keys on $_GET:

$getVars = array_keys($_GET);
Sign up to request clarification or add additional context in comments.

2 Comments

This will show the variables on the URL that invokes the script, not a URL within the script.
@Ignacio Yes, that's what I assume the question is asking about. If not, well, mario's given the solution, and I've upvoted his answer.
5

If this isn't about the current URL, but just some $url string you want to extract the parameters from then:

parse_str(parse_url($url, PHP_URL_QUERY), $params);

will populate $params with:

[topic] => blog
[discussion_id] => 12

1 Comment

+1 And array_keys can then be used to get the keys in an array if necessary.
3

Use the following code to grab data from the URL using GET. Change it to $_POST will work for post.

<?php
foreach ( $_GET as $key => $value ) 
{
        //other code go here
    echo 'Index : ' . $key . ' & Value : ' . $value;
    echo '<br/>';
}
?>

Comments

1

$_GET is usual php-array. you may use it in foreach loop:

foreach ($_GET as $k => $v)
  echo ($k . '=' . $v);

Comments

0

It's an array:

print_r($_GET);

Fetch the elements as you would with any other array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.