note 97190 deleted from function.urlencode by danbrown

From: Date: Wed, 07 Apr 2010 16:14:32 +0000
Subject: note 97190 deleted from function.urlencode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-167851@lists.php.net to get a copy of this message
Note Submitter: frx dot apps at gmail dot com ---- I wrote this simple function that creates a GET query (for URLS) from an array: <?php function encode_array($args) { if(!is_array($args)) return false; $c = 0; $out = ''; foreach($args as $name => $value) { if($c++ != 0) $out .= '&'; $out .= urlencode("$name").'='; if(is_array($value)) { $out .= urlencode(serialize($value)); }else{ $out .= urlencode("$value"); } } return $out . "\n"; } ?> If there are arrays within the $args array, they will be serialized before being urlencoded. Some examples: <?php echo encode_array(array('foo' => 'bar')); // foo=bar echo encode_array(array('foo&bar' => 'some=weird/value')); // foo%26bar=some%3Dweird%2Fvalue echo encode_array(array('foo' => 1, 'bar' => 'two')); // foo=1&bar=two echo encode_array(array('args' => array('key' => 'value'))); // args=a%3A1%3A%7Bs%3A3%3A%22key%22%3Bs%3A5%3A%22value%22%3B%7D ?>

« previous php.notes (#167851) next »