Sobes.tech
Junior — Middle

Is it possible to extract and store parameter names from URL query parameters into a data array?

sobes.tech AI

Answer from AI

Yes, in PHP you can extract parameter names from a URL query and store them in an array. To do this, you can use the superglobal array $_GET, which contains key-value pairs of the query parameters. To get only the parameter names, simply get the keys of this array:

// Example URL: example.com/page.php?name=John&age=30&city=Moscow
$paramNames = array_keys($_GET);
print_r($paramNames); // Outputs: ['name', 'age', 'city']

If you need to process an arbitrary URL, you can use the parse_url and parse_str functions:

$url = 'http://example.com/page.php?name=John&age=30&city=Moscow';
$parts = parse_url($url);
$params = [];
if (isset($parts['query'])) {
    parse_str($parts['query'], $params);
}
$paramNames = array_keys($params);
print_r($paramNames); // ['name', 'age', 'city']