php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #65479 Accessing key -1 should return last value of array
Submitted: 2013-08-19 18:33 UTC Modified: 2013-08-19 19:01 UTC
From: jordandoyle133 at gmail dot com Assigned:
Status: Wont fix Package: Arrays related
PHP Version: Irrelevant OS: All
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jordandoyle133 at gmail dot com
New email:
PHP Version: OS:

 

 [2013-08-19 18:33 UTC] jordandoyle133 at gmail dot com
Description:
------------
Accessing minus keys should start from the end of the array much like the behavior 
in Perl or Python 

Test script:
---------------
<?php
echo [1, 2, 3][-1] . PHP_EOL;
echo [3, 2, 1][-2] . PHP_EOL;
echo ['foo', 'bar'][-1] . PHP_EOL;

Expected result:
----------------
3
2
bar


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-08-19 19:01 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2013-08-19 19:01 UTC] nikic@php.net
This is not possible, because -1 is a valid array key. Consider:

    echo [-1 => -1, 0, 1, 2][-1]; // int(-1)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 23:01:26 2024 UTC