2013-02-19 88 views
1

我有一个包含大约60本书,我需要使用PHP到目前为止我的代码显示了所有的书放进升序从borrowedcount但不会进行排序的XML文件?任何帮助将是非常appriecated排序XML文件按升序使用PHP

PHP

<?php 


$xmlassignDoc = new DOMDocument(); 
$xmlassignDoc->load("books.xml"); 


$books = $xmlBookDoc->getElementsByTagName("item"); 

foreach($books as $list) 
{ 
    $course = $list->getElementsByTagName("course"); 
    $course = $course->item(0)->nodeValue; 


//HERE is where the GET function will be 
    if ($course == "CC150") 
    { 

    print_r($array); 
     $id = $list->getAttribute("id"); 
     echo "<b>Book ID: </b> $id <br>"; 

     $title = $list->getElementsByTagName("title"); 
     $title = $title->item(0)->nodeValue; 
     echo "<b>Title: </b> $title <br>"; 

     $isbn = $list->getElementsByTagName("isbn"); 
     $isbn = $isbn->item(0)->nodeValue; 
     echo "<b>ISBN: </b> $isbn <br>"; 

     $borrowed = $list->getElementsByTagName("borrowedcount"); 
     $borrowed = $borrowed->item(0)->nodeValue; 
     echo "<b>Borrowed Count: </b> $borrowed <br>"; 
     echo "<br>"; 
    } 
} 
//print $xmlBookDoc->saveXML(); 
?> 

xml文件

<?xml version="1.0" encoding="utf-8"?> 
<bookcollection> 
<items> 
    <item id="51390"> 
    <title>Management of systems development /</title> 
    <isbn>0091653215</isbn> 
    <url>http://library.hud.ac.uk/catlink/bib/51390</url> 
    <borrowedcount>45</borrowedcount> 
    <courses> 
     <course>CC140</course> 
     <course>CC210</course> 
    </courses> 
    </item> 
    <item id="483"> 
    <title>Database systems management and design /</title> 
    <isbn>0877091153</isbn> 
    <url>http://library.hud.ac.uk/catlink/bib/483</url> 
    <borrowedcount>28</borrowedcount> 
    <courses> 
     <course>CC140</course> 
    </courses> 
    </item> 
    <item id="585842"> 
    <title>E-learning skills /</title> 
    <isbn>0230573126</isbn> 
    <url>http://library.hud.ac.uk/catlink/bib/585842</url> 
    <borrowedcount>5</borrowedcount> 
    <courses> 
     <course>CC157</course> 
    </courses> 
    </item> 

+0

你不能指望XML文件进行排序如果你不隐式地写一些代码将会对它进行排序。 尝试改变你的那个循环,这样可以节省所有的值到一个数组,然后使用自定义排序功能,您可以选择进行排序的列。 – silkfire 2013-02-19 12:31:22

+0

对不起忘了写,在我曾尝试ksort等,但它不是工作,所以删除代码,那正是我到目前为止 – 2013-02-19 12:34:52

+0

这是什么呢? 'if($ course ==“CC150”)';你是否也根据课程过滤? – 2013-02-19 13:01:56

回答

3

我的解决办法:

$books = array(); 

$xml = simplexml_load_file('books.xml'); 

foreach($xml->items->item as $item) { 
    $books[] = array(
        'id'    => (string)$item->attributes()->id, 
        'title'   => (string)$item->title, 
        'isbn'   => (string)$item->isbn, 
        'course'   => (string)$item->courses->course[0], 
        'borrowed_count' => intval($item->borrowedcount) 
        ); 
} 


array_sort_by_column($books, 'borrowed_count'); 

var_dump($books); 

和排序功能:

function array_sort_by_column(&$array, $column, $direction = SORT_ASC) { 
    $reference_array = array(); 

    foreach($array as $key => $row) { 
     $reference_array[$key] = $row[$column]; 
    } 

    array_multisort($reference_array, $direction, $array); 
} 
+0

的升序那当然书是存在的击球路线的方式在每个借用计数结束在休息,以使其更容易读? – 2013-02-22 12:26:20

+0

array_sort_by_column似乎不是一个可悲的PHP功能? – 2014-08-26 21:09:22

+1

@YannChabot不,这就是为什么我构建了我自己的:)这是PHP的美丽。 – silkfire 2014-08-26 21:24:20

1

文件 '1.PHP':

<?php 
include 'books.php'; 
$b=new books(); 
$arr=$b->load('books.xml');   //1. load books from xml to array 
usort($arr, array('books','cmp')); //2. sort array 
$b->save('out.xml',$arr);   //3. save array to xml 
?> 

文件 'books.php':

<?php 
    class books 
    { 
    //load books from xml to array 
    public function load($fname) 
    { 
     $doc=new DOMDocument(); 

     if($doc->load($fname)) $res=$this->parse($doc); 
     else     throw new Exception('error load XML'); 

     return $res; 
    } 


    static public function cmp($a, $b) 
    { 
     if ($a['fields']['borrowedcount'] == $b['fields']['borrowedcount']) { 
      return 0; 
     } 
     return ($a['fields']['borrowedcount'] < $b['fields']['borrowedcount']) ? -1 : 1; 
    } 


    private function parse($doc) 
    { 
     $xpath = new DOMXpath($doc); 
     $items = $xpath->query("items/item"); 
     $result = array(); 
     foreach($items as $item) 
     { 
      $result[]=array('id'=>$item->getAttribute('id'), 'fields'=>$this->parse_fields($item)); 
     } 
     return $result; 
    } 


    private function parse_fields($node) 
    { 
     $res=array(); 
     foreach($node->childNodes as $child) 
     { 
      if($child->nodeType==XML_ELEMENT_NODE) 
      { 
       $res[$child->nodeName]=$this->get_value($child); 
      } 
     } 

     return $res; 
    } 

    private function get_value($node) 
    { 
     if($node->nodeName=='courses') 
     { 
      $res=array(); 
      foreach($node->childNodes as $child) 
      { 
       if($child->nodeType==XML_ELEMENT_NODE) 
       { 
       $res[]=$child->nodeValue; 
       } 
      } 
      return $res; 
     } 
     else 
     { 
      return $node->nodeValue; 
     } 
    } 

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

     $bc = $doc->appendChild($doc->createElement('bookcollection')); 
     $items = $bc->appendChild($doc->createElement('items')); 

     foreach($rows as $row) 
     { 
      $item=$items->appendChild($doc->createElement('item')); 
      $item->setAttribute('id',$row['id']); 
      foreach($row['fields'] as $field_name=>$field_value) 
      { 
       $f=$item->appendChild($doc->createElement($field_name)); 
       if($field_name=='courses') 
       { 
       foreach($field_value as $course_val) 
       { 
        $course=$f->appendChild($doc->createElement('course')); 
        $course->appendChild($doc->createTextNode($course_val)); 
       } 
       } 
       else 
       { 
       $f->appendChild($doc->createTextNode($field_value)); 
       } 

      } 
     } 

     file_put_contents($fname, $doc->saveXML()); 
    } 

    } 
?>