PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » XML » XML_sql2xml » Bug #1687

Function addArray() generates incorrectly designed XML document

Details

Submitted2004-06-20 19:03 UTC
Fromfog at yandex dot ru
Assignedifeghali
StatusClosed
PackageXML_sql2xml
PHP VersionIrrelevant
Roadmaps(Not assigned)

Comments

[2004-06-20 19:03 UTC] fog at yandex dot ru

Description:
------------
Function addArray() generates incorrectly designed XML document out of not associative arrays.

code

$a = array('apple', 'orange', 'pear');
$xml->addArray($a);
$xml->getXML();

returns incorrect formed XML document, without error reporting

<?xml version="1.0"?>
<root>
<result>
<0>apple</0>
<1>orange</1>
<2>pear</2>
</result>
</root>

I think there should be either an error alert or array processing with the similar function before generating xml document.

function prepare_xml_array($array, $key_name = 'index_', $suffix = '_')
{
if (!is_array($array))
{
return false;
}

$array_new = array();

foreach($array as $key=>$val)
{
if (!is_string($key))
{
$key = $key_name.$key;

while (array_key_exists($key, $array))
{
$key .= $suffix;
}
}

if (is_array($val))
{
$array_new[$key] = prepare_xml_array($val);
}
else
{
$array_new[$key] = $val;
}

}

return $array_new;
}

Reproduce code:
---------------
$a = array('apple', 'orange', 'pear');
$xml->addArray($a);
$xml->getXML();

Expected result:
----------------
some thing like

<?xml version="1.0"?>
<root>
<result>
<i0>apple</i0>
<i1>orange</i1>
<i2>pear</i2>
</result>
</root>

Actual result:
--------------
<?xml version="1.0"?>
<root>
<result>
<0>apple</0>
<1>orange</1>
<2>pear</2>
</result>
</root>