php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28476 string to array variable transformation
Submitted: 2004-05-21 20:10 UTC Modified: 2005-05-26 15:56 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: nepto at platon dot sk Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5CVS, 4CVS (2005-02-03) OS: *
Private report: No CVE-ID: None
 [2004-05-21 20:10 UTC] nepto at platon dot sk
Description:
------------
Transformation of string variable to array variable does not work as expected when prticular assignment is used.

It can be also bug in the var_dump(), but I do not suppose this.

Reproduce code:
---------------
<?php

/*
 * string-to-array.php
 *
 * Developed by Ondrej Jombik <nepto@platon.sk>
 * Copyright (c) 2004 Platon SDG, http://platon.sk/
 * Licensed under terms of GNU General Public License.
 * All rights reserved.
 *
 * Changelog:
 * 2004-05-10 - created
 *
 */

/* $Platon$ */

$str = 'string';

$ar['key'] = $str;
var_dump($ar['key']); // string(6) "string"

$ar['key']['sub-key'] = $ar['key'];
var_dump($ar['key']); /*  string(6) "string"
                          but expected is:
                                array(1) {
                                      ["sub-key"]=>
                                            string(6) "string"
                                }
*/

$ar['key'] = array('sub-key' => $ar['key']);
var_dump($ar['key']); /* now OK:
                            array(1) {
                                  ["sub-key"]=>
                                        string(6) "string"
                            }
*/

/* Modeline for ViM {{{
 * vim: set ts=4:
 * vim600: fdm=marker fdl=0 fdc=0:
 * }}} */

?>

Expected result:
----------------
Written in the code.

Actual result:
--------------
Written in the code as well.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-05-21 21:00 UTC] pollita@php.net
Implicit array creation ONLY occurs if the target variable is either (A) not set, or (B) empty.  In this case the target variable is already set to a non-blank string so it qualifies neither of those criteria.

It seems to me like this should throw a notice since, as you state, the results are very much unexpected.

===================================================================
RCS file: /repository/Zend/Attic/zend_execute.c,v
retrieving revision 1.316.2.34
diff -u -r1.316.2.34 zend_execute.c
--- Zend/zend_execute.c 1 Apr 2004 22:05:38 -0000       1.316.2.34
+++ Zend/zend_execute.c 21 May 2004 18:59:27 -0000
@@ -780,6 +780,9 @@
                                array_init(container);
                                break;
                }
+       } else if ((container->type == IS_STRING || container->type == IS_BOOL) &&
+                               (type == BP_VAR_RW || type == BP_VAR_W)) {
+               zend_error(E_NOTICE, "Implicit array creation failed: Target variable contains non-empty string or boolean true value.");
        }

        switch (container->type) {



While this is considered, I'd suggest explicitly initializing your arrays before pushing data onto them.
 [2005-05-26 15:29 UTC] dmitry@php.net
This is not a bug. Look careful that do you do.

<?php
$ar["key"] = "string";
$ar["key"]["syb-key"] = "string"; // this line is eqivalent to $ar["key"][0] = "s";
$ar["key"]["syb-key"] = "XXX";
var_dump($ar["key"]); // string(6) "Xtring"
?>

All work as expected.
 [2005-05-26 15:56 UTC] nepto at platon dot sk
Oh, yes, $ar["key"]["syb-key"] is transformed to $ar["key"][0]  ("syb-key" to 0), all is OK, sorry for confusion.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Jan 25 04:03:37 2022 UTC