Bug #70733 [Opn->Dup]: int/string conversion bug in arrays

[email protected]
Newsgroups php.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=70733&edit=1

 ID:                 70733
 Updated by:         [email protected]
 Reported by:        bfanger at gmail dot com
 Summary:            int/string conversion bug in arrays
-Status:             Open
+Status:             Duplicate
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   El Capitan
 PHP Version:        irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

Duplicate of bug #66173


Previous Comments:
------------------------------------------------------------------------
[2015-11-28 11:23:21] bfanger at gmail dot com

So the bug is even older, when you use `(array)` instead of `get_object_vars()` :-(
Sound like an opportunity to fix it in PHP7 which suppose to have better types. ;-)

I've found some workarounds make the following code work reliably across php versions:


$data["200"] = false;


Workaround 1: 
  $data = unserialize(serialize((array) $object));

Workaround 2: 
  $data = unserialize(serialize(get_object_vars($object)));

Workaround 3:
  $data = json_decode(json_encode($object), true);

------------------------------------------------------------------------
[2015-10-18 11:35:18] [email protected]

that is get_object_vars now reuse the same array of the properties table of stdclass..

like: 
<?php
$object = json_decode('{"200": true}');
$data = (array)($object);
var_dump($data);
$data['200'] = false;
var_dump($data);
echo json_encode($data, JSON_PRETTY_PRINT);


this will result the same result both on php-5.6 and php7

:<

------------------------------------------------------------------------
[2015-10-18 09:29:39] bfanger at gmail dot com

@laruence But the code snippet doesn't use integers anywhere, but php7 somehow decides to use the integer 200 instead of the given '200'.

PHP 5.3, 5.3, 5.4, 5.5, 5.6 doesn't have this issue.

------------------------------------------------------------------------
[2015-10-18 07:19:48] [email protected]

actually, this is not something new...

because, there are string "200" and interge 200 in the same array.

which is an knew issue.

------------------------------------------------------------------------
[2015-10-17 16:27:45] [email protected]

Reproduced even with 5.5.

Thanks.

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


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=70733


--
Edit this bug report at https://bugs.php.net/bug.php?id=70733&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.