dougan0529
dougan0529
2012-11-01 16:53

挑战性问题 - 使用PHP进行XML数据排序

I have xml file that contains huge amount of Product data. I need to sort my products according to my field(ProductRange)'s data.

$ProductRange=urldecode($_GET['Range']);

XML file data:

<?xml version="1.0" standalone="yes"?>
<Rows>
<Row Code="10026" Name="Eden 36cm Shallow Round Planter Terracotta"  
ProductRange="Eden;3" ProductSubRange="EDEN 36CM ROUND PLANTER"  />
<Row Code="10031" Name="Lid only for 30l Crystal and Uni Clear" 
ProductRange="Crystal;410;Uni;3" ProductSubRange="30Ltr LID"    />
<Row Code="10088" Name="Casa Hipster Laundry Basket Silver" ProductRange="Casa;2"  
ProductSubRange="CASA HIPSTER LAUNDRY BASKET" />
<Row Code="10091" Name="Casa Hipster Laundry Basket Ice White" 
ProductRange="Casa;8;Laundry;1" ProductSubRange="CASA HIPSTER LAUNDRY BASKET"   />
<Row Code="10200" Name="Eden Grid Cover Black" ProductRange="Eden;8" 
ProductSubRange="EDEN DRAIN TIDY/GRID COVER" />
<Row Code="10249" Name="Lid only for 50L Uni Assorted" ProductRange="Uni;1" 
ProductSubRange="50Ltr MAXI BOX LID"  />
<Row Code="10259" Name="Uni 50L Box Clear" ProductRange="Uni" ProductSubRange="MAXIBOX" />
<Row Code="10269" Name="Eden 46cm Shallow Round Planter Terracotta"       
ProductRange="Eden;1" ProductSubRange="EDEN 46CM ROUND PLANTER"  />
</Rows>

The Field "ProductRange" contains the value/order i.e ProductRange="{Range of Product};{Sorting Order of Product it should appear under ProductRange}". For example, the product with Code="10031" and ProductRange="Crystal;410;Uni;3" should appear under both Crystal with Sorting ASCENDING order 410 and Uni with sorting order 3 respectively.And ProductRange without semicolon such as ProductRange="Uni" can appear in any order. Please Note a ProductRange can have multiple products as indicated in XML with semicolon.

Question: How can i sort my products by splitting the ProductRange field using PHP so that it could generate results like in this Ascending sorting order :

Eden:

  Code: 10269         Name: Eden 46cm Shallow Round Planter Terracotta
  Code: 10026         Name: Eden 36cm Shallow Round Planter Terracotta 
  Code: 10200         Name: Eden Grid Cover Black

Uni:

 Code=10259          Name: Uni 50L Box Clear
 Code:10249          Name: Lid only for 50L Uni Assorted 
 Code:10031          Name: Lid only for 30l Crystal and Uni Clear

Crystal:

  Code:10031         Name: Lid only for 30l Crystal and Uni Clear

Casa:

  Code:10088         Name: Casa Hipster Laundry Basket Silver
  Code="10091"       Name: Casa Hipster Laundry Basket Ice White

and so on.....

How can I extract data in this order? Kindly help me.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douzhulv1699 douzhulv1699 9年前

    Your previous function would do just fine all you need is minor changes

    Change

                    case 'ProductRange' :
    

    To

                    case 'ProductRange' :
                    case 'Name' :
    

    Or Simply remove the switch statement

    Then call

    $list = groupBy($xml, "Name", array("show" => true,"delimiter" => " ","name" => "Eden"));
    print("<pre>");
    foreach ( $list as $name => $value ) {
        foreach ( $value as $element ) {
            printf("Code:%s         Name: %s
    ",$element['Code'],$name);
        }
    }
    

    Output

    Code:10026         Name: Eden 36cm Shallow Round Planter Terracotta
    Code:10200         Name: Eden Grid Cover Black
    Code:10269         Name: Eden 46cm Shallow Round Planter Terracotta
    
    点赞 评论 复制链接分享