php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #45959 Object to array conversion leads to weird behaviour
Submitted: 2008-08-31 13:52 UTC Modified: 2008-11-05 09:36 UTC
From: manchokapitancho at gmail dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.2.6 OS: windows xp
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: manchokapitancho at gmail dot com
New email:
PHP Version: OS:

 

 [2008-08-31 13:52 UTC] manchokapitancho at gmail dot com
Description:
------------
Object to array conversion leads to weird behaviour - access to array members is not possible.

Reproduce code:
---------------
$input = json_decode ('{"is_paid":{"0":"4","1":"3"},"is_bank_transfer":{"4":"1"}}');
$arr = ((array)$input->is_bank_transfer);

var_dump($arr);
var_dump($arr[4]);
var_dump($arr['4']);

$keys = array_keys ($arr);
var_dump($keys);
var_dump($arr[$keys[0]]);

Expected result:
----------------
array(1) {
  ["4"]=>
  string(1) "1"
}
string(1) "1"
string(1) "1"
array(1) {
  [0]=>
  string(1) "4"
}
string(1) "1"

Actual result:
--------------
array(1) {
  ["4"]=>
  string(1) "1"
}
NULL
NULL
array(1) {
  [0]=>
  string(1) "4"
}
NULL



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-08-31 13:53 UTC] manchokapitancho at gmail dot com
Also array_key_exists returns false instead of true.
 [2008-11-03 21:56 UTC] felipe@php.net
Fixing that implies a perfomance decrease, hence the better seems be keep it as an known issue, but documented.
 [2008-11-04 08:18 UTC] manchokapitancho at gmail dot com
Isn't this problem quite a serious issue since JSON data transfer mechanism is growing in popularity and manipulating a JavaScript (or JSON) key/value object as a PHP array is a common use case?
 [2008-11-05 07:53 UTC] kalle@php.net
You could always use the 'assoc' parameter on the json_decode() function to avoid this completely:

$input = json_decode('{"is_paid":{"0":"4","1":"3"},"is_bank_transfer":{"4":"1"}}', true);
$arr = $input['is_bank_transfer'];

var_dump($arr);
var_dump($arr[4]);
var_dump($arr['4']);

$keys = array_keys ($arr);
var_dump($keys);
var_dump($arr[$keys[0]]);


:)
 [2008-11-05 09:36 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"integer properties are unaccessible" at http://www.php.net/manual/en/language.types.array.php#language.types.array.casting
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Oct 16 05:01:27 2024 UTC