Pass array to url


/ Published in: PHP
Save to your folder(s)

This code comes from the php documentation (see url)


Copy this code and paste it in your HTML
  1. <?php
  2. function encode_array($args)
  3. {
  4. if(!is_array($args)) return false;
  5. $c = 0;
  6. $out = '';
  7. foreach($args as $name => $value)
  8. {
  9. if($c++ != 0) $out .= '&';
  10. $out .= urlencode("$name").'=';
  11. if(is_array($value))
  12. {
  13. $out .= urlencode(serialize($value));
  14. }else{
  15. $out .= urlencode("$value");
  16. }
  17. }
  18. return $out . "\n";
  19. }
  20. ?>
  21.  
  22. If there are arrays within the $args array, they will be serialized before being urlencoded.
  23.  
  24. Some examples:
  25. <?php
  26. echo encode_array(array('foo' => 'bar')); // foo=bar
  27. echo encode_array(array('foo&bar' => 'some=weird/value')); // foo%26bar=some%3Dweird%2Fvalue
  28. echo encode_array(array('foo' => 1, 'bar' => 'two')); // foo=1&bar=two
  29. echo encode_array(array('args' => array('key' => 'value'))); // args=a%3A1%3A%7Bs%3A3%3A%22key%22%3Bs%3A5%3A%22value%22%3B%7D
  30. ?>

URL: http://www.php.net/manual/en/function.urlencode.php#97191

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.