views:

51

answers:

2

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

A: 

Have a look at this similar thread:

http://stackoverflow.com/questions/2119686/sorting-an-array-of-simplexml-objects

Nev Stokes
I used the xsort() function and got the following error:Warning: array_multisort() [function.array-multisort]: Argument #3 is expected to be an array or a sort flag in /home/usr/public_html/feeds/deadlines.php on line 8Which relates to:array_multisort($sort_proxy, $order, $nodes);
Dr. DOT
Thanks Nev, but that thread do not handle XML when processed via simplexml_load_file().
Dr. DOT
Still looking for a solution, if anyone out there has tried to sort XML files. Thanks.
Dr. DOT
I do not think it is possible to sort XML files in PHP. I have tried usort() and array_multisort() and neither seem to handle the job.
Dr. DOT
The accepted solution to the similar thread I posted works perfectly for me.
Nev Stokes
It just not work when you load an xml file via simplexml_load_file(). It works when you hard code the building of the array as Josh Davis did in his solution. I need something more dynamic in order to process XML files maintained by the end user. So I am still working on finding/building a solution in PHP that can sort XML files.
Dr. DOT
A: 

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
Couldn't you do this with xpath? `$xml->xpath('/deadlines/deadline/date')`
Robin
Robin, that would only give you an array of dates, not the sorted deadline nodes that the OP is after.
Nev Stokes
Dr. DOT
BRILLIANT YOU ARE NEV!!!! "$nodes = $xml->xpath('/deadlines/deadline');" was the missing link. Thanks for helping me.
Dr. DOT