Bug #75328 [Com]: Unable to typecast numeric array key to string

From: Date: Sat, 07 Oct 2017 00:49:01 +0000
Subject: Bug #75328 [Com]: Unable to typecast numeric array key to string
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-211560@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75328&edit=1 ID: 75328 Comment by: bradyn at bradynpoulsen dot com Reported by: langebroek at gmail dot com Summary: Unable to typecast numeric array key to string Status: Not a bug Type: Bug Package: Arrays related Operating System: Any PHP Version: Irrelevant Block user comment: N Private report: N New Comment: I appears to be working as expected and documented. "Strings containing valid decimal integers, unless the number is preceded by a + sign, will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer." - https://secure.php.net/manual/language.types.array.php Previous Comments: ------------------------------------------------------------------------ [2017-10-06 18:28:56] requinix@php.net Since arrays are naturally keyed by integers, having integers and integer strings be distinct would create havoc. I don't see any reason why an integer or string key should matter in your case. Just treat the key/column name like it was a string and it will act like a string. ------------------------------------------------------------------------ [2017-10-06 18:18:14] langebroek at gmail dot com I'm using it to validate certain things in the data layer (SQL abstraction). array( 'col1', 'col2', 'col3' ) xor array( 'col1' => 'val1', 'col2' => 'val2', 'col3' => 'val3' ) It could happen that someone has a legitimate *SQL column with the name '1' but instead it being treated as an index. It's not the end of the world, I can think of an other way. Is there any reason why PHP does this? It'd be a shame if I were to base my validation on the fact that it ensures this and it is changed in the future. [Bug] can be closed, I spose. ------------------------------------------------------------------------ [2017-10-06 18:18:10] langebroek at gmail dot com I'm using it to validate certain things in the data layer (SQL abstraction). array( 'col1', 'col2', 'col3' ) xor array( 'col1' => 'val1', 'col2' => 'val2', 'col3' => 'val3' ) It could happen that someone has a legitimate *SQL column with the name '1' but instead it being treated as an index. It's not the end of the world, I can think of an other way. Is there any reason why PHP does this? It'd be a shame if I were to base my validation on the fact that it ensures this and it is changed in the future. [Bug] can be closed, I spose. ------------------------------------------------------------------------ [2017-10-06 17:43:32] requinix@php.net > it seems impossible to have a numeric string as an array key That is correct, and PHP takes explicit measures to make sure it cannot happen. What is your use case? ------------------------------------------------------------------------ [2017-10-06 17:40:16] langebroek at gmail dot com Description: ------------ Rather self explanatory; it seems impossible to have a numeric string as an array key. For the project I'm working on this is almost a necessity. Test script: --------------- $buf=array((string)1=>'x',"2"=>'y','3'=>'z'); #var_dump($buf);//all int var_dump(array_keys($buf));//clarify all int Expected result: ---------------- In at least one case I'd expect to see a string in the array key, or have an alternative method to achieve such. In some cases it is unwise to typecast at a later time. Actual result: -------------- array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75328&edit=1

« previous php.bugs (#211560) next »