amesguich
amesguich

Reputation: 31

PHP: How to sort XML alphabetically by attribute before "for each" loop

The setup:

I have an external XML file I have no control over, here is it:

001.xml

<?xml version="1.0" encoding="utf-8"?>
<Gallery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ID="001" Name="001">
<Images>
<ImageGalleryEntry Name="1 John" Description="Doe" FileName="830358.1john.png" Width="100" Height="100">
<S3Key>accounts/b2d174c1-a408-4d58-9d48-1490ac4e9f9b/site-37857/cms-assets/images/830358.logooraclehealthsciencesthumb.png</S3Key>
<S3ThumbnailKey>accounts/b2d174c1-a408-4d58-9d48-1490ac4e9f9b/site-37857/cms-assets/thumbnails/830358.logooraclehealthsciencesthumb.png</S3ThumbnailKey>
<ContentLength>1309</ContentLength>
</ImageGalleryEntry>
<ImageGalleryEntry Name="11 Jane" Description="Doet" FileName="943702.11jane.png" Width="100" Height="100">
<S3Key>accounts/b2d174c1-a408-4d58-9d48-1490ac4e9f9b/site-37857/cms-assets/images/943702.logorochethumb.png</S3Key>
<S3ThumbnailKey>accounts/b2d174c1-a408-4d58-9d48-1490ac4e9f9b/site-37857/cms-assets/thumbnails/943702.logorochethumb.png</S3ThumbnailKey>
<ContentLength>1403</ContentLength>
</ImageGalleryEntry>
<ImageGalleryEntry Name="10 Jack" Description="Smith" FileName="965501.10jack.png" Width="100" Height="100">
<S3Key>accounts/b2d174c1-a408-4d58-9d48-1490ac4e9f9b/site-37857/cms-assets/images/965501.pwclogo.png</S3Key>
<S3ThumbnailKey>accounts/b2d174c1-a408-4d58-9d48-1490ac4e9f9b/site-37857/cms-assets/thumbnails/965501.pwclogo.png</S3ThumbnailKey>
<ContentLength>7021</ContentLength>
</ImageGalleryEntry>
</Images>
</Gallery>

I also have a php code that takes this info and puts it out in html nicely:

index.php fragment

<?php
$url = '001.XML';
$xml = simplexml_load_file($url);
foreach($xml->Images->ImageGalleryEntry as $item) {
    echo $item['FileName'] . "<br/>" . $item['Name'] . "<br/>" . $item['Description'] . "<br/><br/>";
};
?>

The output is:

830358.1john.png
1 John
Doe

943702.11jane.png
11 Jane
Doet

965501.10jack.png
10 Jack
Smith

The problem is that I actually need it ordered by the Name attribute, like this:

830358.1john.png
1 John
Doe

965501.10jack.png
10 Jack
Smith

943702.11jane.png
11 Jane
Doet

Note that the XML constantly changes, so the php should automatically order them alphabetically by Name. I'm guessing I need to somehow sort alphabetically all the ImageGalleryEntry tags by their Name attribute before entering the for each loop, but no clue how to do that.

Please provide an actual way to do it, I'm new to php, and won't know what to do if you say something like "make an array".

Upvotes: 2

Views: 7295

Answers (1)

Janci
Janci

Reputation: 3234

You need to use usort. This should work:

<?php
$url = '001.XML';
$xml = simplexml_load_file($url);

$items = array();
foreach($xml->Images->ImageGalleryEntry as $item) {
    $items[] = $item;
};

// Custom sort on the names of the items:
usort ($items, function($a, $b) {
    return strcmp($a['Name'], $b['Name']);
});

foreach ($items as $item) {
    echo $item['FileName'] . "<br/>" . $item['Name'] . "<br/>" . $item['Description'] . "<br/><br/>";
}

Upvotes: 5

Related Questions