Hi Guys im trying to write a function that will convert a xml to array with simplexml. I know there are functions out there that are already written but i need to write this myself. This xml is just a example and can possibly be different format so i need this function to be used with any type of xml structure basically go through all the tags and loop through all of them at all levels and create the array.
Heres what my xml looks like
<approval id='x34445454'>
<p_data>
<p_wide>3.5</p_wide>
<p_hieight>2</p_hieight>
</p_data>
<bars></bars>
<timer>
<loc_time>
<year>2013</year>
<month>4</month>
<day>2</day>
</loc_time>
<start>
<year>2013</year>
<month>04</month>
<day>02</day>
</start>
<end>
<year>2013</year>
<month>04</month>
<day>09</day>
</end>
</timer>
<customer_data>
<custid>8789</custid>
<email>[email protected]</email>
<description>Some item description</description>
<optout>false</optout>
</customer_data>
<large>2</large>
<job name='ord1'>
<guides></guides>
<preview>
<type>mytpe</type>
<info_message></info_message>
<label>both</label>
<index>100</index>
<imagebox>
<data>703.448275862069</data>
<height>11.068965517241</height>
<shift>0</shift>
</imagebox>
<OK_Screen></OK_Screen>
</preview_file>
<preview_file>
<type>mytpe3</type>
<info_message></info_message>
<label>sides</label>
<index>100</index>
<imagebox>
<data>111.448275862069</data>
<height>11.554</height>
<shift>0</shift>
</imagebox>
<OK_Screen></OK_Screen>
</preview>
</job>
<job name='ord1'>
<guides></guides>
<preview>
<type>mytpe</type>
<info_message></info_message>
<label>both</label>
<index>100</index>
<imagebox>
<data>703.448275862069</data>
<height>11.068965517241</height>
<shift>0</shift>
</imagebox>
<OK_Screen></OK_Screen>
</preview_file>
<preview_file>
<type>mytpe3</type>
<info_message></info_message>
<label>sides</label>
<index>100</index>
<imagebox>
<data>111.448275862069</data>
<height>11.554</height>
<shift>0</shift>
</imagebox>
<OK_Screen></OK_Screen>
</preview>
</job>
So far iv written this function
function xmltest($xml) {
$arr = array();
foreach($xml as $element) {
$arr[$element->getName()]=array();
foreach($element->attributes() as $key=>$value) {
$arr[$element->getName()][$key]=(string)$value;
}
foreach($element as $key=>$value) {
$arr[$element->getName()][$key]=array();
if(!$value->children() && (!empty($value))) {
$arr[$element->getName()][$key]=(string)$value;
}
}
}
return $arr;
}
which is returning
Array
(
[approval] => Array
(
[id] => x34445454
[p_data] => Array
(
)
[bars] => Array
(
)
[timer] => Array
(
)
[customer_data] => Array
(
)
[large] => 2
[job] => Array
(
)
)
)
which is great but now i need to continuously loop through it and add all the tags until there are no more.