我也是初学,你看这样行不行<?php
 $doc = new DOMDocument;
$doc->load('book.xml');
$book=$doc->documentElement; 
$airline =$book->getElementsByTagName('airline'); 
 
foreach($airline as $c) 

    $childnodes = $c->getElementsByTagName('c'); 
    $arr = array();
    $i = 0;
    foreach($childnodes as $childc)
    { 
      $arr[$i] = $childc->getAttribute('p');
      $i++;
    }
}
sort($arr);
for($i = 0;$i < count($arr);$i++){
echo $arr[$i]."<br>";
}
?>

解决方案 »

  1.   

    你这个排序这个一个可以啊,但是<c b="F" d="100" p="1700" s="A" />我这个里面这个几个属性都要解析出来,解析出来后,在按照P这个从小-大排序!
      

  2.   

    <?php 
    $doc = new DOMDocument; 
    $doc->load('book.xml'); 
    $book=$doc->documentElement; 
    $airline =$book->getElementsByTagName('airline'); foreach($airline as $c) 

        $childnodes = $c->getElementsByTagName('c'); 
        $arr = array(); 
        $i = 0; 
        foreach($childnodes as $childc) 
        { 
          $arr[$i] = $childc->getAttribute('p');
      $b=$childc->getAttribute('b'); 
      $d=$childc->getAttribute('d');
      $p=$childc->getAttribute('p');
      $s=$childc->getAttribute('s');   
          $i++; 
      echo $p."|".$b."|". $d."|". $p."|".$s."<br>";
        } 


    //这样情况下怎么按照P来排序了?从小-大排序?
    ?>
      

  3.   

    <?php 
    $doc = new DOMDocument; 
    $doc->load('book.xml'); 
    $book=$doc->documentElement; 
    $airline =$book->getElementsByTagName('airline'); foreach($airline as $c) 

        $childnodes = $c->getElementsByTagName('c'); 
        $arr = array(); 
        $i = 0; 
        foreach($childnodes as $childc) 
        { 
          $p = $childc->getAttribute('p');
          $b=$childc->getAttribute('b'); 
          $d=$childc->getAttribute('d');
          $p=$childc->getAttribute('p');
          $s=$childc->getAttribute('s');   
          $i++; 
          echo $p."|".$b."|". $d."|". $p."|".$s."<br>";
        } 


    //这样情况下怎么按照P来排序了?从小-大排序?
    ?>
      

  4.   

    <?php
     $doc = new DOMDocument;
    $doc->load('book.xml');
    $book=$doc->documentElement; 
    $airline =$book->getElementsByTagName('airline'); 
     
    foreach($airline as $c) 

        $childnodes = $c->getElementsByTagName('c'); 
        $arr = array();
        $ListP = array();
        $i = 0;
        foreach($childnodes as $childc)
        { 
          $list = array();
          $ListP[$i] = $childc->getAttribute('p');
          $list[0] = $childc->getAttribute('b'); 
          $list[1] = $childc->getAttribute('d');
          $list[2] = $childc->getAttribute('p');
          $list[3] = $childc->getAttribute('s');
          $arr[$i] = $list;
          $i++;
        }
    }
    sort($ListP);
    for($i = 0;$i < count($ListP);$i++){
    for($j = 0;$j < count($arr);$j++){
    $b = $arr[$j][0];
    $d = $arr[$j][1];
    $p = $arr[$j][2];
    $s = $arr[$j][3];
    if($ListP[$i] == $p){
    echo $p."|".$b."|". $d."|". $p."|".$s."<br>";
    }
    }
    }
    ?>
      

  5.   


    <?php
    $doc = new DOMDocument;
    $doc->load('book.xml');
    $book=$doc->documentElement; 
    $airline =$book->getElementsByTagName('airline'); 
     
    foreach($airline as $c) 

        $childnodes = $c->getElementsByTagName('c'); 
        $arr = array();
        $ListP = array();
        $i = 0;
        foreach($childnodes as $childc)
        { 
          $list = array();
          $ListP[$i] = $childc->getAttribute('p');
          $list[0] = $childc->getAttribute('b'); 
          $list[1] = $childc->getAttribute('d');
          $list[2] = $childc->getAttribute('p');
          $list[3] = $childc->getAttribute('s');
          $arr[$i] = $list;
          $i++;
        }
    }
    sort($ListP);
    for($i = 0;$i < count($ListP);$i++){
    for($j = 0;$j < count($arr);$j++){
    $b = $arr[$j][0];
    $d = $arr[$j][1];
    $p = $arr[$j][2];
    $s = $arr[$j][3];
    if($ListP[$i] == $p){
    echo $p."|".$b."|". $d."|". $p."|".$s."<br>";
    }
    }
    }
    ?>
      

  6.   

    krsort($arr);按照键名逆序排序 
      

  7.   

    <?
    $doc = new DOMDocument; //xml?始解析
    $doc->load("book.xml");
    $book=$doc->documentElement; 
    $airline =$book->getElementsByTagName('airline'); 
       $list = array();
       $index = 0; 
    foreach($airline as $c) 

     $childnodes = $c->getElementsByTagName('c');    for($i=$childnodes->length-1; $i>=0;$i--) {
                        $nixiang=$childnodes->item($i)->GetAttributeNode('p')->nodeValue;
                          if ($nixiang!="0")
                          {
                          $p=$childnodes->item($i)->GetAttributeNode('p')->nodeValue;
                          }
                          $list[$index] = $p;
                          $index++;
                          }}
    sort($list); 
    for($i = 0; $i < count($list);$i++ ){
    echo $list[$i]."<br>";
    }
    ?>