php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #73125 array declaration shouldn't allow same key multiple times
Submitted: 2016-09-20 11:20 UTC Modified: 2021-03-09 13:20 UTC
Votes:2
Avg. Score:1.0 ± 0.0
Reproduced:0 of 2 (0.0%)
From: sami dot vento at codeworks dot fi Assigned:
Status: Suspended Package: Arrays related
PHP Version: 7.0.11 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: sami dot vento at codeworks dot fi
New email:
PHP Version: OS:

 

 [2016-09-20 11:20 UTC] sami dot vento at codeworks dot fi
Description:
------------
Defining array with single clause should check duplicate keys and at least warn user. Current implementation just takes last defined value for key.

Test script:
---------------
$a = array('a'=>3, 'a'=>4);
var_dump($a);


Expected result:
----------------
notice, warning or error

Actual result:
--------------
array(1) {
  ["a"]=>
  int(4)
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-12-12 09:26 UTC] daniele dot maggio at dotnext dot it
Sorry but I think it's not a bug;

it is correct that your value is overwrited since you use a text key, The matter is how you use things.
 [2018-12-12 17:12 UTC] requinix@php.net
Personally, I would be disappointed to see this implemented as I sometimes use it in place of a long if or ternary, like

$value = [
  true => "default", // lowest priority
  $some_condition => "some value",
  $other_condition => "other value",
  $final_condition => "final value" // highest priority
][true];
 [2021-03-09 13:20 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2021-03-09 13:20 UTC] cmb@php.net
> Personally, I would be disappointed to see this implemented …

Indeed, that may break valid code.  Anybody who is interested in
this feature, please pursue the RFC process[1].  For the time
being, I suspend this ticket.

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 22:01:27 2024 UTC