0

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!

Artimis
  • 35
  • 4
  • I would advice json encoding the array, and the decoding it on the other end. It will be much easier than doing manipulation of indices yourself. – thatidiotguy Dec 07 '12 at 17:50
  • http://stackoverflow.com/a/5098410/1253747 . tldr: use serialize() and unserialize() – hndr Dec 07 '12 at 17:56
  • Also, try not to use spaces in file-names. Technically, it's fine, but it's bad practice. Use camel case (MyLibrary) or underscores (My_Library) instead. – Felix Guo Dec 07 '12 at 18:05

1 Answers1

1

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));
Ivan Hušnjak
  • 3,493
  • 3
  • 20
  • 30