1

I have tried to get an XML file to sort and have had no luck. After a day and a-half, I need some help from an expert. Thanks.

My XML File (shortened for the example):

<?xml version="1.0" encoding="iso-8859-1"?>
<deadlines>
    <deadline>
        <date>2010-06-01</date>
        <text>Application for Summer Due</text>
    </deadline>
    <deadline>
        <date>2010-07-01</date>
        <text>Application for Fall Due</text>
    </deadline>
    <deadline>
        <date>2010-07-31</date>
        <text>Summer Bill Due</text>
    </deadline>
</deadlines>

My PHP:

<?php

$xml = simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/feeds/deadlines.xml');

// start THIS WORKS
echo'<pre>';
foreach($xml as $deadline) echo <<<EOF
    Date: {$deadline->date}
    Text: {$deadline->text}


EOF;
echo'</pre>';
// end THIS WORKS

?>

Does anyone have a simple PHP solution to sort the XML file on "date" prior to the echo to screen?

Thanks

H. Ferrence
  • 7,906
  • 31
  • 98
  • 161
  • possible duplicate of [Sorting an array of SimpleXML objects ](http://stackoverflow.com/questions/2119686/sorting-an-array-of-simplexml-objects) – Gordon Oct 22 '10 at 19:06
  • Thanks Gordon, but it's not a duplicate. I tested the code at "Sorting an array of SimpleXML objects" and none of it works. The solutions there do not handle XML when processed via simplexml_load_file(). – H. Ferrence Oct 26 '10 at 13:03
  • Still looking for a solution, if anyone out there has tried to sort XML files. Going on 4 days at this :( Thanks. – H. Ferrence Oct 26 '10 at 13:06

1 Answers1

5

Okay, sorry for going around the houses before - I've added a different answer for clarity but using the sort proxying technique I linked to.

function xsort(&$nodes, $child_name, $order=SORT_ASC)
{
    $sort_proxy = array();

    foreach ($nodes as $k => $node) {
        $sort_proxy[$k] = (string) $node->$child_name;
    }

    array_multisort($sort_proxy, $order, $nodes);
}

$structure = '<?xml version="1.0" encoding="utf-8" ?>
<deadlines>
    <deadline>
        <date>2010-06-01</date>
        <text>Application for Summer Due</text>
    </deadline>
    <deadline>
        <date>2010-07-01</date>
        <text>Application for Fall Due</text>
    </deadline>
    <deadline>
        <date>2010-07-31</date>
        <text>Summer Bill Due</text>
    </deadline>
</deadlines>';

$xml = simplexml_load_string($structure);
$nodes = $xml->xpath('/deadlines/deadline');

// Sort by date, descending
xsort($nodes, 'date', SORT_DESC);
var_dump($nodes);
Nev Stokes
  • 9,051
  • 5
  • 42
  • 44
  • Couldn't you do this with xpath? `$xml->xpath('/deadlines/deadline/date')` – Robin Oct 26 '10 at 16:50
  • Robin, that would only give you an array of dates, not the sorted deadline nodes that the OP is after. – Nev Stokes Oct 26 '10 at 17:26
  • Nev...just got back from lunch and copy & pasted your code and ran it verbatim. It works. (But you already knew that :) So I think I can move it forward and tweak it from here -- ie., I need to be able to read in the XML. I see you have it hard code assigned to the $var. I will work on it and post back shortly. Hopefully I can click the green answer button on this bad boy. – H. Ferrence Oct 26 '10 at 18:10
  • BRILLIANT YOU ARE NEV!!!! "$nodes = $xml->xpath('/deadlines/deadline');" was the missing link. Thanks for helping me. – H. Ferrence Oct 26 '10 at 18:17
  • @Nev Since `->xpath` returns an array, not an object, there's no need to use a proxy, you can just use `usort` with an appropriate callback. Or am I missing something? – IMSoP Oct 17 '12 at 18:15
  • @NevStokes this bit of code has saved me hours of reading took me a while to adapt for my development but it works a treat – Artful_dodger Oct 03 '13 at 22:21