比方说,你已经得到了所有已经有3210个元素作为迭代器。首先,它转换成一个数组,所以我们可以使用数组功能:
$comics = iterator_to_array($comics, 0);
那么你一定要基于一定的价值这个数组排序,在<issuenr>
孩子,这里的价值。这可以用usort
完成和一个回调函数的帮助:
$success = usort($comics, function($a, $b) {
return strnatcmp($a->issuenr, $b->issuenr);
});
回调函数只挑选你想与对方比较具体的值,并将它传递给strnatcmp
这是我评论的自然顺序比较以上。
下面的代码示例演示了如何列出符合特定搜索字母,natsort
版和独特(没有重复的名字,分组)的所有系列。
搜索和分组都与一个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的代码。
对于排序 - 如以前的评论中所建议的 - 使用自然顺序。请参阅http://stackoverflow.com/a/8989994/367456 – hakre
@hakre - 谢谢。我确实看到了。但是,根据我所知,natsort使用它在数组项中找到的第一个文本对其进行排序。从上面的示例节点可以看到,系列名称不是节点中的第一个文本。此外,我不知道如何让它抓住问题编号。 – Batfan