Sorting and grouping SimpleXML Data

自古美人都是妖i 提交于 2019-12-02 06:07:07
hakre

Let's say you've got all <comic> elements as an iterator already. First of all convert it to an array so we can use the array functions:

$comics = iterator_to_array($comics, 0);

Then you want to sort this array based on some value, here the value of the <issuenr> child. This can be done with usort and the help of a callback function:

$success = usort($comics, function($a, $b) {
    return strnatcmp($a->issuenr, $b->issuenr);
});

The callback function just picks the concrete values you want to compare with each other and passes it along to strnatcmp which is the natural order comparison I commented above.


The following code-example shows how to list all series that match a specific search letter, natsorted and distinct (no duplicate names, grouped).

The search and the grouping is both done with an xpath query:

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

This will then output the sorted list:

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

In the next step you want to list all comics in that series, that would be an inner 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);
    }
}

Which will then fetch the comics for each series, output:

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

The code of the xpath_string function can be found in another answer of mine.

Baba

You can use

$key = "id" ;
$iterator = new SimpleXMLIterator($xml);
$array = json_decode(json_encode($iterator), TRUE);
__xsort($array['comic'],"id") ;
var_dump($array['comic']);

Output

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 USed

$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 Function Used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!