Req #53038 [Opn->Sus]: Enhancement / change to array_flip() with boolean values

From: Date: Mon, 07 Dec 2020 16:07:51 +0000
Subject: Req #53038 [Opn->Sus]: Enhancement / change to array_flip() with boolean values
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-230912@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=53038&edit=1

 ID:                 53038
 Updated by:         cmb@php.net
 Reported by:        phplists at stanvassilev dot com
 Summary:            Enhancement / change to array_flip() with boolean
                     values
-Status:             Open
+Status:             Suspended
 Type:               Feature/Change Request
 Package:            Arrays related
 PHP Version:        5.3.3
 Block user comment: N
 Private report:     N

 New Comment:

This feature would have very limited usefulness, since in case of
more than two boolean values, elements would inevitably
overwritten.  If you, or anybody else, still feels that this would
be a generally useful improvement, please write to the internals
mailing list to start a discussion.  For the time being, I'm
suspending this ticket.


Previous Comments:
------------------------------------------------------------------------
[2010-10-10 22:55:59] 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) { }


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=53038&edit=1


Thread (2 messages)

« previous php.bugs (#230912) next »