note 54439 added to function.urlencode

From: Date: Mon, 04 Jul 2005 16:14:47 +0000
Subject: note 54439 added to function.urlencode
Groups: php.notes 
Request: Send a blank email to php-notes+get-91432@lists.php.net to get a copy of this message
I just came across the need for a function that exports an array into a query string. Being able to use urlencode($theArray) would be nice, but here's what I came up with: <?php function urlencode_array( $var, // the array value $varName, // variable name to be used in the query string $separator = '&' // what separating character to use in the query string ) { $toImplode = array(); foreach ($var as $key => $value) { if (is_array($value)) { $toImplode[] = urlencode_array($value, "{$varName}[{$key}]", $separator); } else { $toImplode[] = "{$varName}[{$key}]=".urlencode($value); } } return implode($separator, $toImplode); } ?> This function supports n-dimensional arrays (it encodes recursively). ---- Manual Page -- http://www.php.net/manual/en/function.urlencode.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54439 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54439&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54439&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54439&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54439&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54439&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54439&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54439&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#91432) next »