douzhan8395 2016-04-29 15:38
浏览 22
已采纳

发送POST数组和内存泄漏

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 url_alias[id] 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.

  • 写回答

1条回答 默认 最新

  • dongou2019 2016-04-29 16:00
    关注

    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(
             "a",
             "b",
        6 => "c",
             "d",
    );
    

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

    array(4) {
      [0]=>
      string(1) "a"
      [1]=>
      string(1) "b"
      [6]=>
      string(1) "c"
      [7]=>
      string(1) "d"
    }
    

    Please read the manual to learn more.

    UPDATE

    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 form data is encoded, sent to PHP and the decoding process is on PHP side.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法