比方說,你已經得到了所有已經有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