ZF-5643: Zend_Config_Writer_Xml creates illegal nodes if key is numeric

Issue Type: Bug Created: 2009-01-27T09:54:45.000+0000 Last Updated: 2009-05-22T01:04:34.000+0000 Status: Resolved Fix version(s): - 1.7.7 (16/Mar/09)

Reporter: Kyle Mizell (pimpinonline) Assignee: Ben Scholzen (dasprid) Tags: - Zend_Config_Writer

Related issues: Attachments:


If you try to create an XML config using a multidimensional array ( say from a database select ), and the keys are numeric, it generates an XML config that cannot be read into Zend_Config_Xml ( because it has illegal nodes ).

I believe a fix could be as such:

protected function addBranch(Zend_Config $config, SimpleXMLElement $xml) { foreach ($config as $key => $value) { if ($value instanceof Zend_Config) { if (is_numeric($key)) { $child = $xml->addChild('node' . $key); } else { $child = $xml->addChild($key); } $this->_addBranch($value, $child); } else { $xml->addChild($key, (string) $value); } } }


Posted by Ben Scholzen (dasprid) on 2009-03-09T03:47:18.000+0000

Adding prefixes is not a good solution, as the user doesn't expect it.

Actually, Zend_Config supports numeric arrays with any key-order, thus it allows you to fill in the database result without any problem. Zend_Config_Xml now supports numeric arrays as well, not with any key-order by repeating an element multiple times, which will result in an numeric array starting from 0 to X.

So we have those two options:

a) Zend_Config_Writer_Xml sees a numeric key and throws an exception. b) Zend_Config_Writer_Xml looks up the current array, and if it finds a numeric key, it ignores the key value itself and just creates the numeric array in XML, so that Zend_Config_Xml can read it. Tho this will loose the numeric indexes.

Any suggestions? Personally I'd use case b).

Posted by Christoph, René Pardon (renepardon) on 2009-05-22T00:41:53.000+0000


I get the same error as described above:

Catchable fatal error: Object of class Zend_Config could not be converted to string in /usr/share/php/Zend/Config/Writer/Xml.php on line 182

I tried to overwrite my config.xml file with multidimensional array included like:

This is the code i used:

            $pathtoconfig = realpath(dirname(__FILE__) . '/../../');
            $config = new Zend_Config_Xml($pathtoconfig . '/config.xml', null, array('skipExtends' => true, 'allowModifications' => true));

// $configarray = $config->toArray(); // $config = new Zend_Config($configarray,true); $config->newtry = array();

    $config->setExtend('newtry', 'default');

    $config->newtry->templateid = 2;
    $config->newtry->layout = 'lachmichwech';

    $config->newtry->sendmail = array();
    $config->newtry->sendmail->reseller = array();

    $config->newtry->sendmail->reseller->adress = array('name' => 'firstname lastname, 'mail' => '');

    $writer = new Zend_Config_Writer_Xml();
    $writer->setFilename($pathtoconfig . '/test.xml');


Thank you for your help.


Posted by Christoph, René Pardon (renepardon) on 2009-05-22T01:04:34.000+0000

Hello again,

we (our company) has fixed the problem now. Here is the solution:

// FIXME Zend/Config/Writer/Xml.php line (182) $child = $parent->addChild($branchName -,(string) $value-);

Now it's possible to write childs with numeric keys and array values.


Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.