Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Best way to send array with URL using PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 117
    Comment on it

    Hello Reader's! If you want to send an array as a Param with the url then you can use the serialize and deserialize option from PHP Lets see the example below:-

    <?php
    $array["a"] = "Thusitha";
    $array["b"] = "Sumanadasa";
    $array["c"] = "Lakmal";
    $array["d"] = "Nanayakkara";
    $str = serialize($array);
    $strenc = urlencode($str);
    print $str . "\n";
    print $strenc . "\n";
    ?> 
    print $str . "\n"; gives 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
    print $strenc . "\n"; gives
    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
    

    So if you want to pass this $array through URL to page_no_2.php, ex:-

    $url ='http://page_no_2.php?data=".$strenc."';
    To return back to the original array, it needs to be urldecode(), then unserialize(), like this in page_no_2.php:
    Output will like this:-
     array(4) {
      ["a"]=>
      string(8) "Thusitha"
      ["b"]=>
      string(10) "Sumanadasa"
      ["c"]=>
      string(6) "Lakmal"
      ["d"]=>
      string(11) "Nanayakkara"
    }
    

 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: