# Proposed Function: urlencode_array

- Thread: https://externals.io/message/4237
- Original archive: https://news-web.php.net/php.internals/4237
- Started by Sara Golemon on 2003-08-28T21:16:41+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #4237 — Sara Golemon — 2003-08-28T21:16:41+00:00

URL: https://externals.io/message/4237#4237

proto urlencode_array(array formdata [, string numeric_prefix])

Purpose: Generate a form encoded query string from an associative (or
indexed) array.

Example:

  $formdata = array('page'=>'home',

'users'=>array('bob'=>array('fname'=>'Bob','lname'=>'Smith'),

'jdoe'=>array('fname'=>'Jane','lname'=>'Doe')),
                               'errors'=>array('missing date','missing
time','sums not =='),
                                'other data');

  echo urlencode_array($formdata);
  /* outputs the following

page=home&users[bob][fname]=Bob&users[bob][lname]=Smith&users[jdoe][fname]=J
ane&users[jdoe][lname]=Doe&errors[0]=missing+date&errors[1]=missing+time&err
ors[2]=sums+not+%3D%3D&0=other+data
 */

Example 2:

  Same array as above but encoded using a prefix for numeric values:

  echo urlencode_array($formdata, 'misc_');

page=home&users[bob][fname]=Bob&users[bob][lname]=Smith&users[jdoe][fname]=J
ane&users[jdoe][lname]=Doe&errors[0]=missing+date&errors[1]=missing+time&err
ors[2]=sums+not+%3D%3D&misc_0=other+data


Note Example 2 allows numerically indexed arrays to generate query strings
which will yield legal variable names, but doesn't enforce such behavior.
Also, the numeric_prefix only applies to the root array's indexes since
subsequence arrays may be indexed without compromise.

The patch is available at:
http://frankenbox.alphaweb.net/test/urlencode_array.diff.txt

The underlying function php_url_encode_hash() is meant for later use in
http_request() and for handling a context option in the http:// wrapper, but
it seems as though there's no harm exporting it to userland. (Hence the
addition of urlencode_array() -- I'm flexible on the name, array_urlencode()
has been suggested as well)

I'm looking for a couple +1s before I actually commit this though.

-Sara

---

## #4238 — Ilia Alshanetsky — 2003-08-28T21:42:35+00:00

URL: https://externals.io/message/4237#4238 · In reply to: #4237

I all my time coding PHP I have yet to encounter where such a function would 
be useful. It most cases, it would be simpler to generate the final data 
rather then 2 source arrays, which then will be used to generate the final 
query string. The one possible issue that I see is people using 
urlencode_array($_GET) and forgetting about things like magic_quotes_gpc 
resulting in data corruption etc... Many people also do quite a bit of input 
validation and usually re-use the same code to build the query string for 
subsequent pages.

-0.5

Ilia
 


> proto urlencode_array(array formdata [, string numeric_prefix])
>
> Purpose: Generate a form encoded query string from an associative (or
> indexed) array.
>
> Example:
>
>   $formdata = array('page'=>'home',
>
> 'users'=>array('bob'=>array('fname'=>'Bob','lname'=>'Smith'),
>
> 'jdoe'=>array('fname'=>'Jane','lname'=>'Doe')),
>                                'errors'=>array('missing date','missing
> time','sums not =='),
>                                 'other data');
>
>   echo urlencode_array($formdata);
>   /* outputs the following
>
> page=home&users[bob][fname]=Bob&users[bob][lname]=Smith&users[jdoe][fname]=
>J
> ane&users[jdoe][lname]=Doe&errors[0]=missing+date&errors[1]=missing+time&er
>r ors[2]=sums+not+%3D%3D&0=other+data
>  */
>
> Example 2:
>
>   Same array as above but encoded using a prefix for numeric values:
>
>   echo urlencode_array($formdata, 'misc_');
>
> page=home&users[bob][fname]=Bob&users[bob][lname]=Smith&users[jdoe][fname]=
>J
> ane&users[jdoe][lname]=Doe&errors[0]=missing+date&errors[1]=missing+time&er
>r ors[2]=sums+not+%3D%3D&misc_0=other+data
>
>
> Note Example 2 allows numerically indexed arrays to generate query strings
> which will yield legal variable names, but doesn't enforce such behavior.
> Also, the numeric_prefix only applies to the root array's indexes since
> subsequence arrays may be indexed without compromise.
>
> The patch is available at:
> http://frankenbox.alphaweb.net/test/urlencode_array.diff.txt
>
> The underlying function php_url_encode_hash() is meant for later use in
> http_request() and for handling a context option in the http:// wrapper,
> but it seems as though there's no harm exporting it to userland. (Hence the
> addition of urlencode_array() -- I'm flexible on the name,
> array_urlencode() has been suggested as well)
>
> I'm looking for a couple +1s before I actually commit this though.
>
> -Sara

---

## #4239 — Curt Zirzow — 2003-08-29T06:07:24+00:00

URL: https://externals.io/message/4237#4239 · In reply to: #4237

* Thus wrote Sara Golemon (pollita@php.net):
> proto urlencode_array(array formdata [, string numeric_prefix])
> 
> Purpose: Generate a form encoded query string from an associative (or
> indexed) array.

Can this not be done easily with array_walk?  

I think this might also encourge people to use extremly long query
strings, which will break when it gets to a certain length. I
believe, off hand, 255 bytes is what is suggested max length for a
query string.

Curt
-- 
"I used to think I was indecisive, but now I'm not so sure."

---

## #4253 — Sara Golemon — 2003-08-29T19:02:28+00:00

URL: https://externals.io/message/4237#4253 · In reply to: #4239

> Can this not be done easily with array_walk?
>
Yes, though the primary purpose here is to have a method to create a
form-encoded query string internally, exporting it to userspace is just a
"why the heck not" side-effect.

> I think this might also encourge people to use extremly long query
> strings, which will break when it gets to a certain length. I
> believe, off hand, 255 bytes is what is suggested max length for a
> query string.
>
For HTTP/1.0 GET query_strings yes (2048 bytes for HTTP/1.1 GET), but for
POST data (which uses the same form encoding) the length of the query string
is not limited.

Note: I don't have a great deal invested in this, if there's no desire to
use this my heart won't be broken.

-Sara

---

## #4254 — Curt Zirzow — 2003-08-30T03:10:58+00:00

URL: https://externals.io/message/4237#4254 · In reply to: #4253

* Thus wrote Sara Golemon (pollita@php.net):
> > […]
> Yes, though the primary purpose here is to have a method to create a
> form-encoded query string internally, exporting it to userspace is just a
> "why the heck not" side-effect.

Ahh.. ok.

> 
> > […]
> For HTTP/1.0 GET query_strings yes (2048 bytes for HTTP/1.1 GET), but for
> POST data (which uses the same form encoding) the length of the query string
> is not limited.

I did have a feeling 255 was a bit small, thanks for clarifying
that for me.

> 
> Note: I don't have a great deal invested in this, if there's no desire to
> use this my heart won't be broken.

My second thought was to perhaps apply this to urlencode so
urlencode would be defined as:
  string urlencode(mixed expression)

Although this sort undefines what exacly urlencode does and might
lead to even more confusion.

Curt
-- 
"I used to think I was indecisive, but now I'm not so sure."

---

## #4360 — Sara Golemon — 2003-09-02T21:22:51+00:00

URL: https://externals.io/message/4237#4360

Since Wez has been talking about an http_request() function (which inspired
the underlying php_url_encode_hash() function), using a name like
http_query_string() (or similar) might suffice for both naming conventions
and suitability.

-Sara

> I find this useful, but the name is a bit confusing because the function
> works differently than a combo of `urlencode()` / `rawurlencode()` and
> `array_map()`. One of my picks is build_query_string() (this doesn't conform
> to the naming convention though.)
>
> > […]

---

## #4363 — Wez Furlong — 2003-09-03T10:26:07+00:00

URL: https://externals.io/message/4237#4363 · In reply to: #4360

http_build_query() sounds about right to me.

--Wez.

"Sara Golemon" <pollita@php.net> wrote in message
news:20030902212305.8873.qmail@pb1.pair.com...
> Since Wez has been talking about an http_request() function (which
inspired
> the underlying php_url_encode_hash() function), using a name like
> http_query_string() (or similar) might suffice for both naming conventions
> and suitability.
>
> -Sara
>
> > […]
conform
> > […]
