Doc #80698 [Opn->Nab]: Add a note for key types may not be honored.

From: Date: Tue, 02 Feb 2021 19:20:51 +0000
Subject: Doc #80698 [Opn->Nab]: Add a note for key types may not be honored.
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-18486@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80698&edit=1 ID: 80698 Updated by: requinix@php.net Reported by: ksteinmetz at officesupply dot com Summary: Add a note for key types may not be honored. -Status: Open +Status: Not a bug Type: Documentation Problem Package: Arrays related Operating System: Ubuntu 18.04.5 LTS PHP Version: 7.4.14 Block user comment: N Private report: N New Comment: https://www.php.net/manual/en/language.types.array.php > The key can either be an int or a string. The value can be of any type. > > Additionally the following key casts will occur: > * Strings containing valid decimal ints, unless the number is preceded by a + > sign, will be cast to the int 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. > * Floats are also cast to ints, which means that the fractional part will be > truncated. E.g. the key 8.7 will actually be stored under 8. > * Bools are cast to ints, too, i.e. the key true will actually be stored under 1 > and the key false under 0. > * Null will be cast to the empty string, i.e. the key null will actually be > stored under "". > * Arrays and objects can not be used as keys. Doing so will result in a warning: > Illegal offset type. Previous Comments: ------------------------------------------------------------------------ [2021-02-02 19:13:48] ksteinmetz at officesupply dot com Description: ------------ --- From manual page: https://php.net/function.array-rand --- As we are moving toward strong predictable typing, it would be nice to at least mention in the documentation that the quoted string integers would be implicitly converted to integers. Of course the fixes in our code are trivial. Type cast the results as needed or insure that the key really looks like a string to php. Test script: --------------- is_string(array_rand(["1" => [2], "3" => [4], "5" => [6]])); !is_string(array_rand([1 => [2], 3 => [4], 5 => [6]])); Expected result: ---------------- true true Actual result: -------------- false true ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=80698&edit=1

« previous php.doc.bugs (#18486) next »