PHP数组效率和内存澄清 [英] PHP Array Efficiency and Memory Clarification

查看:163
本文介绍了PHP数组效率和内存澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在声明PHP中的数组,该指数可以创建无序...即

数组[1] = 1
阵列[19] = 2
阵列[4] = 3

我的问题。在创建一个这样的数组,是在两者之间的空值长度为19?如果我试图得到数组[3]它会来为未定义或抛出一个错误?此外,如何影响记忆。将3指数的内存的被吸收或19?

也是目前开发人员编写了一个脚本3阵列FailedUpdates [] FailedDeletes [] FailedInserts []

是不是更有效的这样做,或做在一个关联数组的情况下,控制多个子阵列

 故障阵列(){
    [更新] =>阵列(){
           [0] => 12
           [1] => 41
                   }
    [删除] =>阵列(){
           [0] => 122
           [1] => 414
           [1] => 43
                   }
    [插入] =>阵列(){
           [0] => 12
                   }
}


解决方案

内存效率刍议是不是真的需要在PHP担心,除非你正在处理的真正巨大的阵列/变数巨大的数字的东西。

PHP中的数组是不是真的像C中的数组++或类似的低级语言;在PHP中数组是一个地图。你有键的列表(其必须是唯一的和所有类型字符串或整数的),和对应于密钥值的列表。所以下面是一个合法的数组:

阵列(0 =>'对接',1 =>'土豆',2 =>'郁金香')

但这样是

阵列(5 =>'我','百里'=> 6,19 =>阵列(-1 =>阵列(),7 =>'工作服') )

在这两种情况下,有3项阵列中,因此,3键和3的值。

在除数组中的键和值,一个阵列可以从另一个由顺序的,其中,键/值对发生区分。如果定义一个数组,以便它具有非负整数作为键,这往往会所期待的顺序。当你使用像结构的foreach()

的顺序事项

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
                   }
}

解决方案

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().

这篇关于PHP数组效率和内存澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆