Cesar Cesar - 1 year ago 85
PHP Question

Sending a POST array and memory leak

When sending an HTTP POST array of text data, for example from a web page with an HTML form and text fields, if we use as the name property of the fields the following array

with id being a positive integer, we receive with PHP:

[url_alias] => Array
[2644] => url-of-product-ref-123
[1] => url-of-product-ref-224

how will the server (Apache Linux, for example) handle the POST data? Will it internally prepare an array with 2645 indexes, with a null pointer to all indexes except the index: 1 and the index: 2644, or will it do in other, optimized way? Is the format above correct in terms of efficiency - it is very comfortable for PHP programming, but I am afraid of memory leakages or something.

Answer Source

From PHP manual:

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

In your example it holds only two keys with one value assigned to each.

The manual has examples on how arrays in PHP behave. For example, if you set up the following array:

$array = array(
    6 => "c",

They keys assigned will be 0, 1, 6 and 7:

array(4) {
  string(1) "a"
  string(1) "b"
  string(1) "c"
  string(1) "d"

Please read the manual to learn more.


You are also asking how arrays are sent over HTTP. They (arrays) are not. According to W3C specification defined here:

The form data set is then encoded according to the content type specified by the enctype attribute of the FORM element.

Each enctype has it's own rules too long and extensive to be listed here. Read it there if you wish. The thing is, when PHP receives the encoded data, it automatically decodes indexed form variable names.

So, to wrap it up, the decoding process is on PHP side.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download