note 90552 added to function.array

From: Date: Mon, 27 Apr 2009 22:49:49 +0000
Subject: note 90552 added to function.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-153868@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. ?> ---- Server IP: 212.124.37.9 Probable Submitter: 89.245.53.84 ---- Manual Page -- http://www.php.net/manual/en/function.array.php Edit -- https://master.php.net/note/edit/90552 Del: integrated -- https://master.php.net/note/delete/90552/integrated Del: useless -- https://master.php.net/note/delete/90552/useless Del: bad code -- https://master.php.net/note/delete/90552/bad+code Del: spam -- https://master.php.net/note/delete/90552/spam Del: non-english -- https://master.php.net/note/delete/90552/non-english Del: in docs -- https://master.php.net/note/delete/90552/in+docs Del: other reasons-- https://master.php.net/note/delete/90552 Reject -- https://master.php.net/note/reject/90552 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#153868) next »