Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Passing an array to the URL

    • 0
    • 1
    • 2
    • 2
    • 0
    • 0
    • 0
    • 0
    • 192
    Comment on it

    Sometime you may need to pass the array in the url. You can do this as following:

    <?php
    $array["a"] = "A";
    $array["b"] = "B";
    $array["c"] = "C";
    $array["d"] = "D";
    
    $str = serialize($array);
    $strenc = urlencode($str); 
    print $str . "\n";
    print $strenc . "\n"; ?> 
    

    This ll give you :

    a:4:{s:1:"a";s:8:"Thusitha";s:1:"b";s:10:"Sumanadasa";s:1:"c";s:6:"Lakmal";s:1:"d";s:11:"Nanayakkara";}
    

    and

    a%3A4%3A%7Bs%3A1%3A%22a%22%3Bs%3A8%3A%22Thusitha%22%3Bs%3A1%3A%22b%22%3Bs%3A10%3A%22Sumanadasa%22%3Bs%3A1%3A%22c%22%3Bs%3A6%3A%22Lakmal%22%3Bs%3A1%3A%22d%22%3Bs%3A11%3A%22Nanayakkara%22%3B%7D
    

    Serialize will give you the storable representation of the array:

    This is useful for storing or passing PHP values around without losing their type and structure.

    $url ='http://page&#95;no&#95;2.php?data=".$strenc."';
    

    If you want to pass this array to some other page in url you need to unserialize the data from url

     <?php
        $strenc2= $&#95;GET['data'];
        $arr = unserialize(urldecode($strenc2));
        var&#95;dump($arr);
      ?>
    

    o/p:

     array(4) {
      ["a"]=>
      string(8) "A"
      ["b"]=>
      string(10) "B"
      ["c"]=>
      string(6) "C"
      ["d"]=>
      string(11) "D"
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: