本文介绍了具有挑战性的问题 - 使用 PHP 对 XML 数据进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大量产品数据的 xml 文件.我需要根据我的字段(ProductRange)的数据对我的产品进行排序.

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 文件数据:

<?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>

ProductRange"字段包含值/顺序,即 ProductRange="{Range of Product};{应该出现在 ProductRange 下的产品排序顺序}".例如,Code="10031" 和 ProductRange="Crystal;410;Uni;3" 的产品应该分别出现在按升序排序的 Crystal 和排序顺序为 3 的 Uni 下.和 ProductRange没有分号,例如 ProductRange="Uni" 可以以任何顺序出现.请注意,一个 ProductRange 可以有多个产品,如 XML 中的分号所示.

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 ProductRangewithout semicolon such as ProductRange="Uni" can appear in any order.Please Note a ProductRange can have multiple products as indicated in XML with semicolon.

问题:我如何通过使用 PHP 拆分 ProductRange 字段来对我的产品进行排序,以便它可以生成这样的升序排序结果:

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 :

伊甸园:

  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

大学:

 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

水晶:

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

卡萨:

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

等等......

如何按此顺序提取数据?请帮助我.

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

推荐答案

你之前的函数就可以了,你只需要做一些小改动

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

改变

                case 'ProductRange' :

                case 'ProductRange' :
                case 'Name' :

或者干脆去掉switch语句

Or Simply remove the switch statement

然后调用

$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\n",$element['Code'],$name);
    }
}

输出

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

这篇关于具有挑战性的问题 - 使用 PHP 对 XML 数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 20:42