Sorting a loop by date

Hi,

I’m using Simple XML to read from an XML file. I’ve done a loop for each event. It gives me all the events back in the order of the XML file. However, instead I am trying to sort events by date. Any idea how I could do this?

Please see my code below. Thanks in advance for your help.

[PHP]$newresult = $result->xpath("//match/matchname[.=’$uniqueteam’]/…");

foreach ($newresult as $newmatchlist) {
			     

	
	$newmatch = trim($newmatchlist->{'matchname'}[0]);
	$sport = $newmatchlist->{'sportname'}[0];
	$tournament = $newmatchlist->{'tournamentname'}[0];
	$time=$newmatchlist->{'matchname'}[0]['dateandtime']; 

}[/PHP]

Give this a try, there’s probably a better way to do it, but this might work. I didn’t test it, so I have no clue.

[php]function sortFunction( $a, $b ) {
return strtotime($a[“time”]) - strtotime($b[“time”]);
}

$newresult = $result->xpath("//match/matchname[.=’$uniqueteam’]/…");

foreach ($newresult as $newmatchlist) {
			     		
	$newmatch = trim($newmatchlist->{'matchname'}[0]);
	$sport = $newmatchlist->{'sportname'}[0];
	$tournament = $newmatchlist->{'tournamentname'}[0];
	$time=$newmatchlist->{'matchname'}[0]['dateandtime']; 

        $data[] = array(
                   "newmatch" => $newmatch,
                   "sport" => $sport ,
                    "tournament" => $tournament,
                    "time" => $time,

}

usort($data, “sortFunction”);
print_r($data);
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service