I Ali
I Ali

Reputation: 41

How To Extract Query Parts Of Url Onto Different Arrays By Separating Keys & Values?

I got this url: http://example.com/cat/subcat?var1=value1&var2=value2

I need to grab the query part's keys and values onto separate arrays. So the $query_string array should contain these values:

var1=value1&var2=value2

Then, I want the $keys array to have these values: var1,var2,

And then, I want the $values array to have these values: value1,value2

Finally, I need to echo each values of the $keys array and $values array.

This is my failing attempt:

<?php
$url = 'http://example.com/cat/subcat?var1=value1&var2=value2';

var_dump(parse_url($url));
$scheme = parse_url($url, PHP_URL_SCHEME);
$user = parse_url($url, PHP_URL_USER);
$pass = parse_url($url, PHP_URL_PASS);
$host = parse_url($url, PHP_URL_HOST);
$port = parse_url($url, PHP_URL_PORT);
$path = parse_url($url, PHP_URL_PATH);
$query_strings = parse_url($url, PHP_URL_QUERY);
$fragment = parse_url($url, PHP_URL_FRAGMENT);

echo $parsed_url = $scheme .'://' .$user .$pass .$host .$port .$path .'?' .$query_strings .$fragment;
echo '<br>';

//var1=value1&var2=value2

$query_strings_array = array();
$query_strings_array[] = implode("&", $query_strings);//Warning: implode(): Invalid arguments passed in C:\xampp\htdocs\Templates\Pagination_Template.php on line 311

foreach ($query_strings_array as $key) 
{
    $query_strings_keys_array[] = implode("=", $query_strings);//Warning: implode(): Invalid arguments passed in C:\xampp\htdocs\Templates\Pagination_Template.php on line 315
    $query_strings_keys[] = $key;
    $query_strings_values[] = $value; //Notice: Undefined variable: value in C:\xampp\htdocs\Templates\Pagination_Template.php on line 317
        
    echo $query_strings_keys[-1];
    echo $query_strings_values[-1];
}
?>

Errors: Warning: implode(): Invalid arguments passed in C:\xampp\htdocs\Templates\Pagination_Template.php on line 311 Warning: implode(): Invalid arguments passed in C:\xampp\htdocs\Templates\Pagination_Template.php on line 315 Notice: Undefined variable: value in C:\xampp\htdocs\Templates\Pagination_Template.php on line 317

I have also mentioned the errors I get on the above code's comments to indicate the faulty lines in my code.

Upvotes: 1

Views: 862

Answers (2)

Samir Selia
Samir Selia

Reputation: 7065

Use parse_url to get query string and parse_str to get each parameter.

<?php
$url = 'http://example.com/cat/subcat?var1=value1&var2=value2';

// Get query string from the URL
$query_string = parse_url($url, PHP_URL_QUERY);

// Split the query string into individual parameters and return it in $arr
parse_str($query_string, $arr);

// Store keys i.e. parameter names in $keys
$keys = array_keys($arr);

// Store values i.e. parameter values in $values
$values = array_values($arr);

// print
print_r($keys);
print_r($values);
?>

Upvotes: 0

Kinglish
Kinglish

Reputation: 23654

First you need to isolate the query string. Then use parse_str to separate your query string into key/value pairs, then array_keys and array_values to get the 2 separate arrays.

<?php
// example code
$url = 'http://example.com/cat/subcat?var1=value1&var2=value2';

$qs = str_replace(explode("?",$url)[0]."?", '', $url);
parse_str($qs, $arr);
$keys = array_keys($arr);
$values = array_values($arr);

print_r($keys);
print_r($values);

Example: https://www.tehplayground.com/9rNqYnVvIdFLsr3d

Array
(
    [0] => var1
    [1] => var2
)
Array
(
    [0] => value1
    [1] => value2
)

Upvotes: 1

Related Questions