views:

61

answers:

3

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
                   }
}
+1  A: 

array[3] will be undefined/unset but not causing an error, and the array will use only memory for that 3 values - php isn't like C where you have to look at those things.

oezi
+3  A: 

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

Hammerite
You can use a C style array by using an SPLFixedArray. Creation time isn't much faster (a few % faster), but access is 20 to 30% faster...
ircmaxell
"If you define an array so that it has nonnegative integers as keys, this will normally be the expected order." The order has nothing to do with the type of the keys, only the order the keys were created. In the example the OP gives, the order will be 1, 19, 3.
Artefacto
Artefacto: Yes, I know. My comment is not intended as a definitive rule, rather as a general principle. A lot of the time when you create an array, you do so by defining the 0th entry, then the 1st, then the 2nd, etc. In that case the keys will be in the expected order. This will also typically be the case when the array is returned by a function that returns arrays indexed by nonnegative integers. If the array elements are defined out of the "natural" order, then yes they will occur in whatever order they were defined.
Hammerite
I changed the word "normally" to "often" to qualifty the statement a little more.
Hammerite
+1  A: 
  • Accessing $arr[3] gives a notice: Notice: Undefined offset: 3 in /data/home/sjoerd/public_html/svnreps/test/a.php on line 3. You can avoid this by checking with isset() or array_key_exists().
  • There are no nulls stored.
  • Having empty elements won't take up extra memory.
  • Whether you should use multiple variables or an array depends on the context and how you use the variables.
Sjoerd