Artimis
Artimis

Reputation: 35

pass an array through the url and get it by GET method

I passed an array through the url but when I want to use $sim_p[1] i encounter with "Notice: Undefined offset: 1" but for offset 0 it works fine!

$url = 'My Library.php?num='.$k.'&sim_p[]=' . implode('&sim_p[]=', array_map('urlencode', $sim_p)); //sim_p[ 0 ] and sim_p[ 1 ] are full in this page
          header ("Location:".$url);

and in My Library.php:

  $num = $_GET[ 'num' ]; 
    $sim_p = array();
    if($num > 0){
    $sim_p = $_GET[ 'sim_p' ]; 
    }

Thank you in advance!

Upvotes: 0

Views: 1701

Answers (1)

Ivan Hušnjak
Ivan Hušnjak

Reputation: 3503

If you have used var_dump($_GET) you would probably get something like this:

"?num=2&sim_p[]=value1&sim_p[]=value2"

array(3) {
  ["num"]=>
  string(1) "2"
  ["sim_p"]=>
  array(1) {
    [0]=>
    string(6) "value1"
  }
  ["amp;sim_p"]=>
  array(1) {
    [0]=>
    string(6) "value2"
  }
}

Note that you implode with & instead of &. This made query resolve into another item in $_GET with key "amp;sim_p".

This will work:

$url = 'My Library.php?num='.$k.'&sim_p[]=' . 
       implode('&sim_p[]=', array_map('urlencode', $sim_p));

Upvotes: 1

Related Questions