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


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); } } }


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).


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.


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.