我的xml文件包含大量的产品数据。我需要根据我的字段(ProductRange)的数据对产品进行排序。
$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 =“{产品范围}; {产品的排序顺序应显示在ProductRange下}”。例如,Code =“10031”和ProductRange =“Crystal; 410; Uni; 3”的产品应分别出现在具有排序ASCENDING订单410的Crystal和排序顺序为3的Uni下。并且ProductRange 没有分号如ProductRange =“Uni”可以按任何顺序出现。 请注意,ProductRange可以包含XML中带分号的多个产品。
问题: 如何通过使用PHP拆分ProductRange字段来对我的产品进行排序,以便它可以生成类似于此升序排序顺序的结果:
伊甸:
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
水晶:
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
依旧.....
如何按此顺序提取数据?请帮助我。
答案 0 :(得分:0)
您以前的功能可以很好地完成您需要的所有细微更改
更改
case 'ProductRange' :
到
case 'ProductRange' :
case 'Name' :
或者只需删除switch语句
然后致电
$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