I researched a lot to convert an xml file to a 2d array in the same way that excel tries to do the same algorithm as excel 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 a two-dimensional array, as if you opened the same file in Excel, it will show you this as

I want to convert to a 2-dimensional array, as Excel does. For now, I can extract shortcuts like Excel.
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 it to be generated 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 ), )
XML Example 2 ..
<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>
XML Example 3 ..
<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>