i had already created multidimensional array from parsed xml file using simplexml with numerical keys but i want them to be named keys instead of numbers .
xml file is as below:
<workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" >
<Worksheet ss:Name="tab1">
<Table>
<Row>
<Cell><Data>Id</Data></Cell> // names which i want to be array keys.
<Cell><Data>Company</Data></Cell> //
<Cell><Data>Year</Data></Cell> //
</Row>
<Row>
<Cell><Data>120</Data></Cell> //values
<Cell><Data>Apple</Data></Cell>
<Cell><Data>2011</Data></Cell>
</Row>
<Row>
<Cell><Data>121</Data></Cell>
<Cell><Data>Samsung</Data></Cell>
<Cell><Data>2010</Data></Cell>
</Row>
</Table>
</Worksheet>
<Worksheet ss:Name="tab2">
<Table>
<Row>
<Cell><Data>Id</Data></Cell>
<Cell><Data>Company</Data></Cell>
<Cell><Data>Year</Data></Cell>
</Row>
<Row>
<Cell><Data>320</Data></Cell>
<Cell><Data>Sony</Data></Cell>
<Cell><Data>2001</Data></Cell>
</Row>
<Row>
<Cell><Data>321</Data></Cell>
<Cell><Data>HTC</Data></Cell>
<Cell><Data>2001</Data></Cell>
</Row>
</Table>
</Worksheet>
</workbook>
and her is my code for parsing xml file and creating array
$xml=simplexml_load_file($fileData);
$result= array();
$i=0;
foreach($xml->Worksheet as $worksheet ):
$result['tab'][$i] = array();
$result['tab'][$i]['name']=(string)$worksheet->attributes("ss", true)->Name;
foreach($worksheet as $table):
$k =0;
unset($table->Row[0]); //removing first row which i want to be keys of array
foreach($table as $row):
foreach($row as $cell):
$result['tab'][$i]['data'][$k][] =(string)$cell->Data;
endforeach;
$k++;
endforeach;
endforeach;
$i++;
endforeach;
return $result;
array which i am getting :
Array
(
[tab] => Array
(
[0] => Array
(
[name] => tab1
[data] => Array
(
[0] => Array
(
[0] => 120 //keys should be name of first row of xml
[1] => Apple
[2] => 2011
)
[1] => Array
(
[0] => 121
[1] => Samsung
[2] => 2010
)
)
)
[1] => Array
(
[name] => tab2
[data] => Array
(
[0] => Array
(
[0] => 320
[1] => Sony
[2] => 2001
)
[1] => Array
(
[0] => 321
[1] => HTC
[2] => 2001
)
)
)
)
)
i want array to be as below :
Array
(
[tab] => Array
(
[0] => Array
(
[name] => tab1
[data] => Array
(
[0] => Array
(
[Id] => 120 // named keys instead of numbers
[Company] => Apple
[Year] => 2011
)
[1] => Array
(
[Id] => 121
[Company] => Samsung
[Year] => 2010
)
)
)
[1] => Array
(
[name] => tab2
[data] => Array
(
[0] => Array
(
[Id] => 320
[Company] => Sony
[Year] => 2001
)
[1] => Array
(
[Id] => 321
[Company] => HTC
[Year] => 2001
)
)
)
)
)
it's a bit long question . but well explained . Thanks.