我正在排序&从XML文件中分组发布数据。我目前使用的方法在大多数情况下正常工作,尽管我觉得有一种更有效的方法可以完成我想要完成的任务。
以下是目标节点的示例:
<comic>
<id>117</id>
<mainsection>
<series>
<displayname>My Amazing Adventure</displayname>
<sortname>My Amazing Adventure</sortname>
</series>
</mainsection>
<issuenr>2</issuenr>
<seriefirstletter>
<displayname>M</displayname>
<sortname>M</sortname>
</seriefirstletter>
</comic>
以下是我正在采取的步骤。
虽然字母排序工作正常。我也想让同一系列中的问题按数字排序。 目前无效。现在,数字排序顺序如下所示:1,10,12,2,3。
我想让数字排序问题得到理顺。我也觉得我目前正在jQuery中进行的分组,可以在PHP中完成,而我正在完成循环。有关更好/更有效处理这些数据的建议,我们将不胜感激。
答案 0 :(得分:1)
您可以使用
$key = "id" ;
$iterator = new SimpleXMLIterator($xml);
$array = json_decode(json_encode($iterator), TRUE);
__xsort($array['comic'],"id") ;
var_dump($array['comic']);
输出
array
0 =>
array
'id' => string '1' (length=1)
'mainsection' =>
array
'series' =>
array
...
1 =>
array
'id' => string '2' (length=1)
'mainsection' =>
array
'series' =>
array
...
2 =>
array
'id' => string '3' (length=1)
'mainsection' =>
array
'series' =>
array
...
3 =>
array
'id' => string '10' (length=2)
'mainsection' =>
array
'series' =>
array
...
4 =>
array
'id' => string '12' (length=2)
'mainsection' =>
array
'series' =>
array
...
使用XML
$xml = "<comics>
<comic>
<id>1</id>
<mainsection>
<series>
<displayname>My Amazing Adventure - 1</displayname>
<sortname>My Amazing Adventure</sortname>
</series>
</mainsection>
</comic>
<comic>
<id>10</id>
<mainsection>
<series>
<displayname>My Amazing Adventure - 10</displayname>
<sortname>My Amazing Adventure</sortname>
</series>
</mainsection>
</comic>
<comic>
<id>12</id>
<mainsection>
<series>
<displayname>My Amazing Adventure 12</displayname>
<sortname>My Amazing Adventure</sortname>
</series>
</mainsection>
</comic>
<comic>
<id>2</id>
<mainsection>
<series>
<displayname>My Amazing Adventure 2</displayname>
<sortname>My Amazing Adventure</sortname>
</series>
</mainsection>
</comic>
<comic>
<id>3</id>
<mainsection>
<series>
<displayname>My Amazing Adventure 3</displayname>
<sortname>My Amazing Adventure</sortname>
</series>
</mainsection>
</comic>
</comics>" ;
答案 1 :(得分:1)
假设您已经将所有<comic>
元素作为迭代器。首先将其转换为数组,以便我们可以使用数组函数:
$comics = iterator_to_array($comics, 0);
然后,您希望根据某个值对此数组进行排序,此处为<issuenr>
子项的值。这可以通过usort
和回调函数的帮助来完成:
$success = usort($comics, function($a, $b) {
return strnatcmp($a->issuenr, $b->issuenr);
});
回调函数只选择您想要相互比较的具体值,并将其传递给strnatcmp
,这是我上面评论的自然顺序比较。
以下代码示例显示了如何列出与特定搜索字母匹配的所有系列,natsort
ed和distinct(没有重复的名称,已分组)。
搜索和分组都是使用xpath
查询完成的:
$searchval = 'T';
$file = 'compress.zlib://comiclist10-12.xml.gz';
$xml = simplexml_load_file($file);
$series = $xml->xpath(
"/*/comiclist/comic[./seriefirstletter/displayname = '$searchval']
/mainsection/series/sortname[
not(. = ../../../following-sibling::comic/mainsection/series/sortname)
]"
);
natsort($series);
foreach($series as $serie)
{
echo $serie, "\n";
}
然后输出排序列表:
Tale of the Batman: Gotham by Gaslight, A
Tales of Suspense: Captain America & Iron Man #1 Commemorative Edition
Tales to Astonish, Vol. 1
Teenage Mutant Ninja Turtles
Teenage Mutant Ninja Turtles Micro Series
Teenage Mutant Ninja Turtles Ongoing
Terminator / Robocop: Kill Human
Thanos
Thing, Vol. 1
Thor, Vol. 2
Thor, Vol. 3
Thor: Blood Oath
Thor: For Asgard
Thor: Man of War
Thor: Son of Asgard
Thor Annual
Thor Corps
Thundercats
Thundercats (DC Comics - Wildstorm)
Thundercats: Enemy's Pride
Tomb of Dracula, Vol. 4, The
Torch, The
Toxin
Transformers: Armada
Transformers: Generation One
Transformers: Infiltration
Truth: Red, White & Black
在下一步中,您要列出该系列中的所有漫画,这将是一个内在的foreach:
foreach ($series as $serie) {
echo $serie, "\n";
$string = xpath_string($serie);
$comics = $serie->xpath("../../../../comic[./mainsection/series/sortname = $string]");
foreach ($comics as $i => $comic) {
printf(" %d. id: %s\n", $i+1, $comic->id);
}
}
然后将获取每个系列的漫画,输出:
Tale of the Batman: Gotham by Gaslight, A
1. id: 8832
Tales of Suspense: Captain America & Iron Man #1 Commemorative Edition
1. id: 3591
Tales to Astonish, Vol. 1
1. id: 3589
Teenage Mutant Ninja Turtles
1. id: 117
Teenage Mutant Ninja Turtles Micro Series
1. id: 13789
Teenage Mutant Ninja Turtles Ongoing
1. id: 13780
2. id: 13782
3. id: 13787
Terminator / Robocop: Kill Human
1. id: 13775
Thanos
1. id: 3597
Thing, Vol. 1
1. id: 3746
Thor, Vol. 2
1. id: 5873
Thor, Vol. 3
1. id: 1035
2. id: 1635
3. id: 2318
4. id: 2430
5. id: 2463
6. id: 3333
7. id: 3616
8. id: 11731
9. id: 11733
Thor: Blood Oath
1. id: 3635
2. id: 3636
Thor: For Asgard
1. id: 11545
2. id: 11546
Thor: Man of War
1. id: 3644
Thor: Son of Asgard
1. id: 538
2. id: 3645
Thor Annual
1. id: 5868
Thor Corps
1. id: 3640
Thundercats
1. id: 209
Thundercats (DC Comics - Wildstorm)
1. id: 3654
Thundercats: Enemy's Pride
1. id: 3649
Tomb of Dracula, Vol. 4, The
1. id: 3719
Torch, The
1. id: 2328
2. id: 2330
3. id: 2461
Toxin
1. id: 3720
Transformers: Armada
1. id: 3737
Transformers: Generation One
1. id: 557
Transformers: Infiltration
1. id: 3729
2. id: 3731
Truth: Red, White & Black
1. id: 3750
2. id: 3751
xpath_string
function can be found in another answer of mine的代码。