note 90552 modified in function.array by johannes
| From: | johannes@php.net | Date: | Wed, 12 Dec 2012 11:36:56 +0000 |
| Subject: | note 90552 modified in function.array by johannes | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192484@lists.php.net to get a copy of this message | ||
I encountered the following but didn't see it documented/reported anywhere so I'll just
mentioned it here.
As written in the manual:
"When index is omitted, an integer index is automatically generated, starting at 0. If index is
an integer, next generated index will be the biggest integer index + 1".
This generated index has always the largest integer used as a key so far. So adding $a[5] =
'foo'; after an $a[10] = 'bar'; will not force the next generated index to be 6
but to be 11 as 10 was the highest index encountered until here.
Anyway, the following can happen:
<?php
$max_int = 2147483647; // Max value for integer on a 32-bit system
$arr = array();
$arr[1] = 'foo'; // New generated index will be 2
$arr[ $max_int ] = 'bar'; // Caution: Next generated index will be -2147483648 due to the
integer overflow!
$arr[0] = 'bar'; // The highest value should be 2147483648 but due to the i-overflow it is
-2147483648 so current index 0 is larger. The new generated index therefore is 1!
$arr[] = 'failure'; // Warning: Cannot add element to the array as the next element is
already occupied.
?>
--was--
I encountered the following but didn't see it documented/reported anywhere so I'll just
mentioned it here.
As written in the manual:
"When index is omitted, an integer index is automatically generated, starting at 0. If index is
an integer, next generated index will be the biggest integer index + 1".
This generated index has always the largest integer used as a key so far. So adding $a[5] =
'foo'; after an $a[10] = 'bar'; will not force the next generated index to be 6
but to be 11 as 10 was the highest index encountered until here.
Anyway, the following can happen:
<?php
$max_int = 2147483647; // Max value for integer on a 32-bit system
$arr = array();
$arr[1] = 'foo'; // New generated index will be 2
$arr[ $max_int ] = 'bar'; // Caution: Next generated index will be -2147483648 due to the
integer overflow!
$arr[0] = 'bar'; // The highest value should be 2147483648 but due to the i-overflow it is
-2147483648 so current index 0 is larger. The new generated index therefore is 1!
$arr[] = 'failure'; // Warning: Cannot add element to the array as the next element is
already occupied.
?>
http://php.net/manual/en/function.array.php