Question:
Php - beginner Question?
anonymous
2007-03-19 20:23:19 UTC
Thanks to everyone who helped with my question on Saturday on getting started with php. This one is still a beginner question but just a little more advanced.

I am working on reading in an XML file and displaying it on a page. I have that part worked out but I want to display the elements in reverse order.

Just as an example say I have a simple XML doc like...

SF 49ers
New York Giants
Miami Dolphins


So I get the element and using the foreach loop I go through each iteration of "team" and print the value. so the output is
SF 49ers
New York Giants
Miami Dolphins

However I would like to output in the reverse so that its...

Miami Dolphins
New York Giants
SF 49ers

Any help on how to do this using php with XML?
Four answers:
Brady
2007-03-19 20:46:28 UTC
despite the negativity the idea above is correct. You can add the elements into an array then use array_reverse() to turn it around. Another options would be to actually move through the list the other direction. You would not be able to use foreach anymore but instead use the list, each method or a backwards for loop if your situation allowed it (if the array has numerical, ordered indexes).
Atif Majid
2007-03-19 23:24:48 UTC
First parse the xml and insert each record in array. And loop the array in reverse order like

(for $i=count($arrValue)-1;$i>=0;$i--)

{

echo $arrValue[$i];

}
undercoloteal
2007-03-19 20:34:24 UTC
I suppose you could add all the strings to an array, and if PHP is remotely useful for anythin (which it's not) it will have a function to reverse an array, although I'm not sure how well that would work because arrays in PHP are actually messed up hybrid array-hashes.
Nuwan Chaturanga
2007-03-19 21:12:16 UTC
I think you can use XSL itself for this matter. The nodes selected by xsl:for-each are normally presented in document

order. To get reverse document order, you want to reverse that positioning -- which can be done by re-sorting in descending order by original position.



XML

=========



first

second







XSL

========







This is the element.





This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...