Page 1 of 1

SimpleXML Reverse

Posted: Wed Feb 28, 2007 1:22 am
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]

Re: SimpleXML Reverse

Posted: Wed Feb 28, 2007 1:33 am
by itsmani1
Mute wrote: I'd like to be able to show the <item> tags in reverse.
means?

Posted: Wed Feb 28, 2007 1:34 am
by daedalus__

Posted: Wed Feb 28, 2007 4:42 am
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.

Posted: Wed Feb 28, 2007 5:08 am
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";
}