I have a multidimensional array like the one below and I need a class with a recursive function to build up an Xml doc with nested elements and using simple XML.
Heres an example of the array:
$array= array(
'name'=>'products',
'category'=>'test category',
array
(
'product_id' => 1 ,
'name' => 'test product',
'price' => 13.99,
'enabled' => 1,
'tagline' => 'updated dbTest tagline',
'url_key' => '',
'description' => '',
'description_short' => '',
array
(
'meta'=> 'blah blah',
'keywords'=>array
(
'keyword1',
'keyword2',
'keyword 3'
)
)
),
array
(
'product_id' => 2,
'name' => 'padlocks',
'price' => 17.00,
'enabled' => 1,
'tagline' => '',
'url_key' => '',
'description' => '',
'description_short' => '',
'extra_info' => ''
),
array
(
'product_id' => 3,
'name' => 'dbTestProduct',
'price' => '',
'enabled' => 1,
'tagline' => 'dbTest tagline',
'url_key' => '',
'description' => '',
'description_short' => '',
'extra_info' => ''
)
);
I have had a go at it and got it working one array level deep. It uses this sort of functionality:
foreach($array as $key => $value)
{
if ( is_numeric($key))
{
$key = $node;
}
// delete any char not allowed in XML element names
$key = preg_replace('/[^a-z0-9\-\_\.\:]/i', '', $key);
if(is_array($value))
{
arrayXml::arrayToXml($value);
}
else
{
$this->_xml->addChild($key, $value);
}
}
Would be great if someone could help. Thanks in advanced
Heres an example of the array:
$array= array(
'name'=>'products',
'category'=>'test category',
array
(
'product_id' => 1 ,
'name' => 'test product',
'price' => 13.99,
'enabled' => 1,
'tagline' => 'updated dbTest tagline',
'url_key' => '',
'description' => '',
'description_short' => '',
array
(
'meta'=> 'blah blah',
'keywords'=>array
(
'keyword1',
'keyword2',
'keyword 3'
)
)
),
array
(
'product_id' => 2,
'name' => 'padlocks',
'price' => 17.00,
'enabled' => 1,
'tagline' => '',
'url_key' => '',
'description' => '',
'description_short' => '',
'extra_info' => ''
),
array
(
'product_id' => 3,
'name' => 'dbTestProduct',
'price' => '',
'enabled' => 1,
'tagline' => 'dbTest tagline',
'url_key' => '',
'description' => '',
'description_short' => '',
'extra_info' => ''
)
);
I have had a go at it and got it working one array level deep. It uses this sort of functionality:
foreach($array as $key => $value)
{
if ( is_numeric($key))
{
$key = $node;
}
// delete any char not allowed in XML element names
$key = preg_replace('/[^a-z0-9\-\_\.\:]/i', '', $key);
if(is_array($value))
{
arrayXml::arrayToXml($value);
}
else
{
$this->_xml->addChild($key, $value);
}
}
Would be great if someone could help. Thanks in advanced