dsxz84851 2010-06-14 14:25 采纳率: 100%
浏览 98
已采纳

PHP数组效率和内存澄清

When declaring an Array in PHP, the index's may be created out of order...I.e

Array[1] = 1 Array[19] = 2 Array[4] = 3

My question. In creating an array like this, is the length 19 with nulls in between? If I attempted to get Array[3] would it come as undefined or throw an error? Also, how does this affect memory. Would the memory of 3 index's be taken up or 19?

Also currently a developer wrote a script with 3 arrays FailedUpdates[] FailedDeletes[] FailedInserts[]

Is it more efficient to do it this way, or do it in the case of an associative array controlling several sub arrays

"Failures" array(){
    ["Updates"] => array(){
           [0] => 12
           [1] => 41
                   }
    ["Deletes"] => array(){
           [0] => 122
           [1] => 414
           [1] => 43
                   }
    ["Inserts"] => array(){
           [0] => 12
                   }
}
  • 写回答

3条回答 默认 最新

  • dsa80833 2010-06-14 14:36
    关注

    Memory effiency isn't really something you need to worry about in PHP unless you're dealing with really huge arrays / huge numbers of variables.

    An array in PHP isn't really like an array in C++ or a similar lower-level language; an array in PHP is a map. You have a list of keys (which must be unique and all of type string or integer), and a list of values corresponding to the keys. So the following is a legal array:

    array(0 => 'butt', 1 => 'potato', 2 => 'tulip')

    but so is

    array(5 => 'i', 'barry' => 6, 19 => array(-1 => array(), 7 => 'smock'))

    In both cases there are 3 entries in the array, hence 3 keys and 3 values.

    In addition to the keys and values in the array, one array may be distinguished from another by the order in which the key/value pairs occur. If you define an array so that it has nonnegative integers as keys, this will often be the expected order. The order matters when you use constructs like foreach().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序