note 24811 rejected from language.types.array by jmcastagnetto

From: Date: Thu, 29 Aug 2002 20:38:55 +0000
Subject: note 24811 rejected from language.types.array by jmcastagnetto
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-36000@lists.php.net to get a copy of this message
It would be good to create array-index ranges and string ranges: > $a[offset, length] would mean the array index range from $a> [offset] to $a[offset + length - 1]. The offset could be negative: $a[-offset, length] would mean the same as: > $a[count($a)-offset, length] if $a is an array, or: > $a[strlen($a)-offset, length] if $a is not an array and can be converted to a string. The length span a larger area than the available indexes in an array or the string length. When dereferenced, an array index range becomes a new array containing the indexed values. The array will contain length elements taken from the starting position indicated by the start key, in the current order of the array. If an array is filled in with integer keys sorted in reverse order, the selected subrange will include in fact keys with decreasing values the first key being [start], then [start-1], and so on... So the subrange length will keep the current ordering of keys. If keys are missing in the source array to fullfill the requested length, more keys are added, the same way as "$array[] = value;" creates new keys with NULL values. If the string does not have enough characters to fullfill the requested length, dereferencing the string subrange would return a truncated string. For assignments, there are two things to consider: 1) a simple = assignment will always mean an array subrange assignement. If the source subrange is a string or a scalar or an object, it will first be converted to an array containing that value: $a[0,1] = "f" will force the conversion of $a into an array, even if its a string or a scalar, resulting in the ([0] => $a) array. Then the array subrange is selected for assignment with the given value. In this example the result in $a will be that $a will be an array where [0] => "f". The following: $a[0,2] = "f" will set [0] => "f" and [1] => "f". 2) operator assignments are possible: if the assigned value is a scalar, the operation is applied to each element of the array in the selected range: $a[0,5] += 1; will increment $a[0], ..., $a[4], first promoting each element to the type of the assigned expression as if we had performed: $a[$key] = $a[$key] + 1; starting at the key element [0], and operating on the 4 next() elements (in the current array order). Here also, if the specified length is too short, the array will be extended before operation as if missing keys were initially containing NULL. 3) the .= string concatenation assignment has a different meaning with index ranges: it forces the conversion of the base ariable into a string and then applies the assignment to the string resulting from this operation. It replaces the selected substring by the string resulting from the assigned value: $a[3,5] .= 10 will then first convert $a and 1 into strings, then it will replace the 5 characters of the converted $a (starting at the 4th character) by the string '10': - if $a contained an array, its conversion would become 'Array', and $a would become this string 'Arr10' (there's no blank padding for the missing characters, as this is a substring replacement) - if $a contained the string 'ABCDEFGHIJ', then after operation it will contain the string 'ABC10IJ'

« previous php.notes (#36000) next »