1

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>

これが私が取っている現在のステップです。

  • SimpleXMLを使用したXMLファイルのロード
  • ターゲットノードを指定し、iterator_to_arrayを使用してそれを配列に変換します
  • seriesname属性を比較する(strcmp)usort関数を使用して、すべてのシリーズをアルファベット順にソートします。
  • 各ページのクエリ文字列を使用してアルファベットの各文字を指定し、クエリ文字列の文字をseriesfirstletter値と比較するIFステートメントを使用しています。したがって、該当するノードのみが返されます。
  • 次に、foreachステートメントを開始します。必要なデータをLIアイテムにエコーアウトします。
  • 最後に、jQueryを使用して各LIアイテムのIDを確認し、視覚的にグループ化します。IDにスペースを削除したシリーズ名を使用するPHP変数を作成しました。グループの上に適切なシリーズ名のH4見出しを挿入し、グループの下に分離DIVを挿入します。

アルファベット順の並べ替えが正しく機能している間。また、同じシリーズ内の問題を数値で並べ替えてほしいと思っています。これは現在機能していません。現在、数値の並べ替え順序は次のようになっています:1、10、12、2、3。

数値ソートの問題を解決したいと思います。また、現在jQueryで行っているグループ化は、ループを実行しているときにPHPで行うことができるように感じます。このデータを処理するためのより良い/より効率的な方法に関するアドバイスをいただければ幸いです。

4

2 に答える 2

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>" ;

使用される__xsort関数

于 2012-10-06T15:39:59.017 に答える
1

<comic>すでにイテレータとしてすべての要素を持っているとしましょう。まず、配列に変換して、配列関数を使用できるようにします。

$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関数のコードは私の別の答えにあります。

于 2012-10-06T15:59:01.600 に答える