简体   繁体   中英

How to return the names of $_GET variables in a url php

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, ie "topic" and "discussion_id from the $_GET variables and can i then store the values: "topic" and "discussion_id" in an array?

你可以通过调用$_GET上的array_keys来实现这个目的:

$getVars = array_keys($_GET);

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

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/>';
}
?>

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

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

It's an array:

print_r($_GET);

Fetch the elements as you would with any other array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM