Get values ​​from XML to PHP array

I wrote a small function to read an XML file using simpleXML and used Pear Benchmark Iterate to run it. I read that simpleXML and the DOM put the entire XML file in memory, which can create a lot of overhead for large files.

The function below is relatively small in size and I only pull 10 values ​​from the XML file.

Iterations of the function below (xml filesize: 200kb) take an average of 2.5 seconds.

Can someone suggest a solution with PHP XMLparser, a class associated with it, or perhaps another efficient way to parse an XML array into an array?

SimpleXML Version

function getItems($file_id, $item_count=5)
{
    switch ($file_id)
    {
        case '1':
        $file = "http://xml_file.xml";

        if ($xml = simplexml_load_file($file)) 
        {
            $i=0;
            foreach ($xml->info as $info) 
            {
                if ($i < $item_count)
                {
                    $var[] = array(
                        "id"  =>  (string)$info->id,
                        "name" => (string)$info->name); 
                }
                $i++;   
            }
            return $var;
        }
    }
}
+3
source share
1 answer

XML Parser , - , . , .

, XML- , , , .

, OO, xml_set_object .

+1

Source: https://habr.com/ru/post/1778323/


All Articles