SimpleXML Reverse

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mute
Forum Newbie
Posts: 15
Joined: Mon Jul 10, 2006 9:02 am

SimpleXML Reverse

Post by Mute »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I'm parsing some XML and then spitting it out on the screen. What I'd like to be able to do is reverse the array that SimpleXML creates but for the life of me, I can't do it. Here's some sample XML.

[syntax="xml"]<league>
<country>England</country>
<name>Premier League</name>
<all_matches>
<item>
  <round>1</round>
  <date>19 Aug 06</date>
  <result>1 - 1</result>
  <team1>Sheffield Utd.</team1>
  <team2>Liverpool</team2>
</item>
<item>
  <round>1</round>
  <date>19 Aug 06</date>
  <result>3 - 1</result>
  <team1>West Ham United</team1>
  <team2>Charlton</team2>
</item>
<item>
  <round>1</round>
  <date>19 Aug 06</date>
  <result>1 - 1</result>
  <team1>Arsenal</team1>
  <team2>Aston Villa</team2>
</item>
</allmatches>
</league>
I'd like to be able to show the <item> tags in reverse.


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Re: SimpleXML Reverse

Post by itsmani1 »

Mute wrote: I'd like to be able to show the <item> tags in reverse.
means?
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Mute
Forum Newbie
Posts: 15
Joined: Mon Jul 10, 2006 9:02 am

Post by Mute »

Well, I'm using the following code to show the the items.

Code: Select all

	

$xml = new SimpleXMLElement('results.xml');

$round = 0;
	foreach ($xml->all_matches->item as $team) 
	{
		if($round != $team->round)
		{
			echo "<tr bgcolor='gray'>";
			echo "<td style='font-weight:bold' colspan='4'>Round ".$team->round;
			echo "</tr>";		
		}
		echo "<tr bgcolor='#CCCCCC'>";
		echo "<td align='center'>".$team->date;
		echo "<td align='center'>".$team->team1;
		echo "<td align='center' style='font-weight:bold'>".$team->result;
		echo "<td align='center'>".$team->team2;
		echo "</tr>";
		$round = $team->round;
	}	
And it then displays....

19 Aug 06 Sheffield Utd. 1 - 1 Liverpool
19 Aug 06 West Ham United 3 - 1 Charlton
19 Aug 06 Arsenal 1 - 1 Aston Villa

I'd like it to show....

19 Aug 06 Arsenal 1 - 1 Aston Villa
19 Aug 06 West Ham United 3 - 1 Charlton
19 Aug 06 Sheffield Utd. 1 - 1 Liverpool

So, is there a way I can reverse the $xml I have on the fly to show that? Sorry if this a really simple question. I tried using array_reverse() and arsort() but neither worked or at least I couldn't get them to work.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Imperfect but working solution

Code: Select all

<?php
$xml = <<< eox
<league>
<country>England</country>
<name>Premier League</name>
<allmatches>
<item>
  <round>1</round>
  <date>19 Aug 06</date>
  <result>1 - 1</result>
  <team1>Sheffield Utd.</team1>
  <team2>Liverpool</team2>
</item>
<item>
  <round>1</round>
  <date>19 Aug 06</date>
  <result>3 - 1</result>
  <team1>West Ham United</team1>
  <team2>Charlton</team2>
</item>
<item>
  <round>1</round>
  <date>19 Aug 06</date>
  <result>1 - 1</result>
  <team1>Arsenal</team1>
  <team2>Aston Villa</team2>
</item>
</allmatches>
</league>
eox;

$doc = simplexml_load_string($xml);
for($i=count($doc->allmatches->item); $i>0; ) {
	$item = $doc->allmatches->item[--$i];
	echo $item->team1, ' : ', $item->team2, "<br />\n";
}
Post Reply