View Single Post
Old 03-24-2017, 11:37 PM  
TitanWM
Confirmed User
 
TitanWM's Avatar
 
Industry Role:
Join Date: Dec 2015
Posts: 111
Why not sort the output? Have you by the assorted xml a performance advantage?

I do not understand what you are doing but this code should work:

PHP Code:
<?php
/////////////////////////////sort xml////////////////////////////////////////////////////
$url 'test.xml';

$sortXML = new cams();

$arr=$sortXML->load($url);

foreach(
$arr->element as $item) {
    
$items[] = $item;
};
usort ($items, function($a$b ) {
    return 
strcmp($a->person->age[0] , $b->person->age[0]);
});

// save array to xml
$sortXML->save('test.xml',$items);

///////////////////////////
// class
//
class cams {

    public function 
load($fname) {
        
$doc = new DOMDocument();

        if(
$doc->load($fname)) {
            
$ressimplexml_load_file($fname);
        } else {
            throw new 
Exception('error load XML');
        }

        return 
$res;
    }

    
//save array to xml
    
public function save($fname$rows) {
        
$doc = new DOMDocument('1.0','utf-8');
        
$doc->formatOutput true;

        
$elements $doc->appendChild($doc->createElement('root'));

        foreach(
$rows as $row){
            
$element=$elements->appendChild($doc->createElement('element'));

            foreach(
$row as $field_name=>$field_value) {
                
$f=$element->appendChild($doc->createElement($field_name));

                if(
$field_name=='about') {

                    foreach(
$field_value as $field_name=>$field_value) {
                        
$description=$f->appendChild($doc->createElement($field_name));
                        
$description->appendChild($doc->createTextNode($field_value));
                    }
                }

                elseif(
$field_name=='person') {
                    foreach(
$field_value as $field_name=>$field_value) {
                        
$description=$f->appendChild($doc->createElement($field_name));
                        
$description->appendChild($doc->createTextNode($field_value));
                    }
                } else {
                    
$f->appendChild($doc->createTextNode($field_value));
                }
            }
        }

        
file_put_contents($fname$doc->saveXML());
    }
}
?>
The code would still be optimized
__________________
CamSoda invite link - promote an unsatisfied cam site!
TitanWM is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote