php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #24159 Php starts indexing from zero if lowest index is negative
Submitted: 2003-06-12 15:38 UTC Modified: 2004-08-07 16:30 UTC
From: rsen010 at ec dot auckland dot ac dot nz Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.3.2, 5.0.0b2-dev OS: Any
Private report: No CVE-ID: None
 [2003-06-12 15:38 UTC] rsen010 at ec dot auckland dot ac dot nz
Description:
------------
Hi,
When I was going through ?Chapter 7.types? in PHP manual I found something confusing regarding the following statement ?Having e.g. the highest index being -6 will result in -5 being the new key? (under Syntax ? specifying with array()).What actually happens is it Php starts indexing from zero if lowest index is negative value unless there is no index of zero. 
But amazing thing is sometimes it omit some values unless.
Just consider the following examples.
Ex:1
$a  = array(-3=>'February', 'March');
print_r($a);
This will prints out the folloiwng
> Array ( [-3] => February [0] => March )

Ex:2
$a  = array( 'January', '0'=>'a','b', 'c',-3=>'February', 'March');
print_r($a);
 This will prints out the folloiwng                
Array ( [0] => a [1] => b [2] => c [-3] => February [3] => March )        
 
 Please be kind to check this out if I?m correct and reply me.
 Thanks
 Naleen


Reproduce code:
---------------
$a  = array(-3=>'February', 'March');
print_r($a);
$a  = array( 'January', '0'=>'a','b', 'c',-3=>'February', 'March');
print_r($a);

Expected result:
----------------
Array (-3=>February, [-2] => March );
Array ( [1]=>January [0] => a [2] => b [3] => c [-3] => February [4] => March )

Actual result:
--------------
Array ([-3] => February [0] => March )
Array ( [0] => a [1] => b [2] => c [-3] => February [3] => March )

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-08-03 19:40 UTC] et@php.net
This behaviour was introduced in 4.3.0 - reclassifying as an array problem.
I don't know if the behaviour or the documentation should be changed.
 [2003-08-04 16:03 UTC] iliaa@php.net
This is a documentation issue. The change was made to avoid possible crashes.
 [2004-08-07 16:30 UTC] vrana@php.net
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at 
http://www.php.net/downloads.php

"As of PHP 4.3.0, the index generation behaviour described above has changed. Now, if you append to an array in which the current maximum key is negative, then the next key created will be zero (0). Before, the new index would have been set to the largest existing key + 1, the same as positive indices are."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 08 03:01:29 2024 UTC