-3

I have got a link mydomain.com/somefile.php?data=1&data=2&data=3. I want to extract all the three datas from the URL. I used the following code :-

echo $_GET['data']." ".$_GET['data']." ".$_GET['data'];

The output of the code was :-

3 3 3

But I want the following output :-

1 2 3

How to proceed? Any help will be appreciated Please...do not mark this question as duplicate... This is different from the previous one and I cannot change the URL

2
  • Please...do not mark this question as duplicate... This is different from the previous one and I cannot change the URL Commented Aug 3, 2013 at 11:53
  • 1
    If you cannot change the URL, PHP will not be able to parse the query string for you. You will need to parse the query string manually: stackoverflow.com/questions/353379/… Commented Aug 3, 2013 at 11:58

1 Answer 1

3

You can't do that. PHP will take only the last data if you pass a simple argument. However, you can pass a array through your URL by using this syntax:

?data[]=1&data[]=2&data[]=3

php:
var_dump($_GET['data']);

Edit

Well, you didn't like my first answer as I can see by this downvote... So to redeem my credibility, I propose you this solution, by parsing directly the query string:

<?php

$qstring = $_SERVER['QUERY_STRING']; // "data=1&data=2&data=3" here
$args = explode('&', $qstring);

$data = array();

for ($i = 0; $i < count($args); $i++) { 
    $param = explode('=', $args[$i]);

    // If the key hasn't been seen yet, add a new array to data collection 
    if(empty($data[$param[0]])) 
        $data[$param[0]] = array();

    // If there's a value given (we can have case like "data=1&test" for example)
    if(!empty($param[1]))
        $data[$param[0]][] = $param[1];
}

print_r($data);
/*
    Array
    (
        [data] => Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
            )

    )
*/

Then you can do:

<?php echo $data['data'][0].' '.$data['data'][1].' '.$data['data'][2]; ?>

Hope I help you this time.

Sign up to request clarification or add additional context in comments.

2 Comments

Maxime, the problem is that I have got the URL from a plugin and I cannot change the URL
So you can't do this in PHP, or you have to parse directly the URL by yourself. Check in $_SERVER variable (I think it's $_SERVER['PHP_SELF'])

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.