php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53038 Enhancement / change to array_flip() with boolean values
Submitted: 2010-10-10 22:55 UTC Modified: 2010-12-01 15:23 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: phplists at stanvassilev dot com Assigned:
Status: Open Package: Arrays related
PHP Version: 5.3.3 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: phplists at stanvassilev dot com
New email:
PHP Version: OS:

 

 [2010-10-10 22:55 UTC] phplists at stanvassilev dot com
Description:
------------
This is not a bug, but I propose a change to how array_flip operates with 
boolean values, in order to improve common use cases, because:

1) The current behavior (warning and completely empty array) renders arrays with 
one or more boolean values invalid for flipping so they have to be manually 
filtered in advance.

2) When true/false are cast to 1/0, all loose type checks against the keys will 
match (i.e. true == 1, false == 0) which makes the cast also a practical one for 
real utility.

3) The change will make the casting behavior of array_flip consistent with the 
*already* existing behavior in the following example:

// casts booleans to int, and produces: array(1 => 123, 0 => 321);
$a = array(); $a[true] = 123; $a[false] = 321;




Test script:
---------------
var_dump(
    array_flip(
        array('foo' => true, 'bar' => false)
    )
);

Expected result:
----------------
array(2) { [1]=> string(3) "foo" [0]=> string(3) "bar" }

Actual result:
--------------
Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER 
values! in ... on line 2

array(0) { }

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-01 15:23 UTC] jani@php.net
-Package: *General Issues +Package: Arrays related -Operating System: Any +Operating System:
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Mon Nov 11 23:01:33 2019 UTC