S
S
Sharov Dmitry2015-03-16 16:02:09
PHP
Sharov Dmitry, 2015-03-16 16:02:09

How to form a structure from an xml file?

I may have worded the title wrong, sorry.
The point is this. We have an xml file of 50-70 megabytes. The general structure of the elements is the same, but there are some differences when there are no elements.
I need to create a complete array matrix that will be obtained when fetching this xml.
This is the only diagram that came to mind.

<?php
$xml = simplexml_load_file('pg.xml');
$array = getData($xml);
print_r($array);

function getData($items, $parent = null){
    $rgData = [];
    if(is_object($items) || is_array($items)){
        foreach($items as $key => $item){
            $tmp = ((is_array($item)) ? getData($item, $key) : $item);

            if(is_numeric($key))
                $rgData[$parent] = $tmp;
            else 
                $rgData[$key] = $tmp;
        }
        return $rgData;
    }
    return $items;
}

But something doesn't turn out right. Pooadyut numeric keys

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Bay, 2015-03-16
@kawabanga

Should the attributes be saved?
what prevents to do (array)$xml ?

F
FanatPHP, 2015-04-01
@FanatPHP

Did you figure it out or do you need more help?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question