I am trying to parse a xml file as below to create a multidimensional array .
<workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" >
<Worksheet ss:Name="45">
<Table>
<Row><Cell><Data>232</Data></Cell></Row>
<Row><Cell><Data>Apple</Data></Cell></Row>
</Table>
</Worksheet>
<Worksheet ss:Name="46 - new">
<Table>
<Row><Cell><Data>876</Data></Cell></Row>
<Row><Cell><Data>samsung</Data></Cell></Row>
</Table>
</Worksheet>
Here is the code thus far.
$xml=simplexml_load_file($xmlfile);
$result= array();
foreach($xml->Worksheet as $worksheet ) {
$result['tab']['name'][]=$worksheet->attributes('ss', TRUE)->Name;
foreach($worksheet as $table){
foreach($table as $row){
foreach($row as $cell){
$result['tab']['units'][]=$cell->Data;
}
}
}
}
print_r($result);
I am trying to get array as below :
items =[
tab=>[
'name' => '45',
'units'=>[
['0'=>'232'],
['1'=>'Apple']
]
]
tab=>[
'name' => '46-new',
'units'=>[
['0'=>'876'],
['1'=>'samsung']
]
]
];
but i'm getting the result as below .
array(
[tab]=>array(
[name]=>array
(
[0]=>SimpleXmlElement Object
(
[0]=> 45
)
[1]=>SimpleXmlElement object
(
[1]=>46-new
)
)
[units]=>array
(
[0]=>SimpleXmlElement Object
(
[0]=> Nr
)
[1]=>SimpleXmlElement object
(
[0]=>model
)
[2]=>SimpleXmlElement Object
(
[0]=> 232
)
[3]=>SimpleXmlElement object
(
[0]=>apple
)
.........
)
)
)
it's quite challenging .How should the code be modified to achieve the goal?