I have researched a lot to convert an xml file to 2d array in a same way excel does trying to make same algorithm as excel does when you open an xml file in excel.
<items>
<item>
<sku>abc 1</sku>
<title>a book 1</title>
<price>42 1</price>
<attributes>
<attribute>
<name>Number of pages 1</name>
<value>123 1</value>
</attribute>
<attribute>
<name>Author 1</name>
<value>Rob dude 1</value>
</attribute>
</attributes>
<contributors>
<contributor>John 1</contributor>
<contributor>Ryan 1</contributor>
</contributors>
<isbn>12345</isbn>
</item>
<item>
<sku>abc 2</sku>
<title>a book 2</title>
<price>42 2</price>
<attributes>
<attribute>
<name>Number of pages 2</name>
<value>123 2</value>
</attribute>
<attribute>
<name>Author 2</name>
<value>Rob dude 2</value>
</attribute>
</attributes>
<contributors>
<contributor>John 2</contributor>
<contributor>Ryan 2</contributor>
</contributors>
<isbn>6789</isbn>
</item>
</items>
I want it to convert it to to 2-dimensional array like if you open the same file in Excel it will show you like this
I want to convert to 2-dimensional array just like Excel does. So far I can extract the labels like Excel does
function getColNames($array) {
$cols = array();
foreach($array as $key=>$val) {
if(is_array($val)) {
if($val['type']=='complete') {
if(in_array($val['tag'], $cols)) {
} else {
$cols[] = $val['tag'];
}
}
}
}
return $cols;
}
$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
Goal
I want to have it generate like this..
array (
0 => array (
'sku'=>'abc 1',
'title'=>'a book 1',
'price'=>'42 1',
'name'=>'Number of Pages 1',
'value'=>'123 1',
'isbn'=>12345
),
1 => array (
'sku'=>'abc 1',
'title'=>'a book 1',
'price'=>'42 1',
'name'=>'Author 1',
'value'=>'Rob dude 1',
'isbn'=>12345
),
2 => array (
'sku'=>'abc 1',
'title'=>'a book 1',
'price'=>'42 1',
'contributor'=>'John 1',
'isbn'=>12345
),
3 => array (
'sku'=>'abc 1',
'title'=>'a book 1',
'price'=>'42 1',
'contributor'=>'Ryan 1',
'isbn'=>12345
),
)
Sample 2 XML..
<items>
<item>
<sku>abc 1</sku>
<title>a book 1</title>
<price>42 1</price>
<attributes>
<attribute>
<name>Number of pages 1</name>
<value>123 1</value>
</attribute>
<attribute>
<name>Author 1</name>
<value>Rob dude 1</value>
</attribute>
</attributes>
<contributors>
<contributor>John 1</contributor>
<contributor>Ryan 1</contributor>
</contributors>
<isbns>
<isbn>12345a</isbn>
<isbn>12345b</isbn>
</isbns>
</item>
<item>
<sku>abc 2</sku>
<title>a book 2</title>
<price>42 2</price>
<attributes>
<attribute>
<name>Number of pages 2</name>
<value>123 2</value>
</attribute>
<attribute>
<name>Author 2</name>
<value>Rob dude 2</value>
</attribute>
</attributes>
<contributors>
<contributor>John 2</contributor>
<contributor>Ryan 2</contributor>
</contributors>
<isbns>
<isbn>6789a</isbn>
<isbn>6789b</isbn>
</isbns>
</item>
</items>
Sample 3 XML..
<items>
<item>
<sku>abc 1</sku>
<title>a book 1</title>
<price>42 1</price>
<attributes>
<attribute>
<name>Number of pages 1</name>
<value>123 1</value>
</attribute>
<attribute>
<name>Author 1</name>
<value>Rob dude 1</value>
</attribute>
</attributes>
<contributors>
<contributor>John 1</contributor>
<contributor>Ryan 1</contributor>
</contributors>
<isbns>
<isbn>
<name>isbn 1</name>
<value>12345a</value>
</isbn>
<isbn>
<name>isbn 2</name>
<value>12345b</value>
</isbn>
</isbns>
</item>
<item>
<sku>abc 2</sku>
<title>a book 2</title>
<price>42 2</price>
<attributes>
<attribute>
<name>Number of pages 2</name>
<value>123 2</value>
</attribute>
<attribute>
<name>Author 2</name>
<value>Rob dude 2</value>
</attribute>
</attributes>
<contributors>
<contributor>John 2</contributor>
<contributor>Ryan 2</contributor>
</contributors>
<isbns>
<isbn>
<name>isbn 3</name>
<value>6789a</value>
</isbn>
<isbn>
<name>isbn 4</name>
<value>6789b</value>
</isbn>
</isbns>
</item>
</items>
See Question&Answers more detail:
os